I built the following c library:
#define _GNU_SOURCE
#include <pthread.h>
#include <sched.h>
int attachCurrentThreadToCore(int core);
int main(void) {
return 0;
}
int attachCurrentThreadToCore(int core) {
pthread_t thread;
thread = pthread_self();
cpu_set_t set;
CPU_ZERO(&set);
CPU_SET(core, &set);
return pthread_setaffinity_np(thread, sizeof(cpu_set_t), &set);
}
and now I am trying to pinvoke attachCurrentThreadToCore
.
This is my pinvoke test code:
[DllImport("/home/anon/Documents/RadFramework/RadFramework.Libraries.Threading/src/TestProject1/bin/Debug/net5.0/pthreadWrapper")]
private static extern int attachCurrentThreadToCore(int core);
[Test]
public void Test1()
{
attachCurrentThreadToCore(0);
while (true)
{
}
}
Now I would expect core 1 on my machine to spin in the while loop. Instead I receive this exception when pinvoking:
Unable to load shared library '/home/anon/Documents/RadFramework/RadFramework.Libraries.Threading/src/TestProject1/bin/Debug/net5.0/pthreadWrapper' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: /home/anon/Documents/RadFramework/RadFramework.Libraries.Threading/src/TestProject1/bin/Debug/net5.0/pthreadWrapper: cannot dynamically load position-independent executable
I think it boils down to "cannot dynamically load position-independent executable".
Is there anything else I need to do when compiling my c library?
Changed the build artifact in eclipse from executable to library:
This results in this build output:
make all Building file: ../src/pthreadWrapper.c Invoking: Cross GCC Compiler gcc -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/pthreadWrapper.d" -MT"src/pthreadWrapper.o" -o "src/pthreadWrapper.o" "../src/pthreadWrapper.c" Finished building: ../src/pthreadWrapper.c
Building target: libPthreadWrapper.so Invoking: Cross GCC Linker gcc -shared -o "libPthreadWrapper.so" ./src/pthreadWrapper.o -lpthread Finished building target: libPthreadWrapper.so
After that the pinvoke is working.