views:

1926

answers:

4

Hi.

I´m pretty new to programming in C++ and I´m using pthreads. I´m cross compiling my code for OpenWRT but for some reason I get segmentation fault when I run the program on my board but it runs fine on my PC. I suspect that the error occurs in the linking stage of the compilation because I tried a small C program and that worked fine. Also if I change the name of the file to .cpp and compile it with g++ it also works.

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

void *run(void *dummyPtr) {
    printf("I am a thread...\n");
    return NULL;
}

int main(int argc, char **argv) {
    printf("Main start...\n");
    pthread_t connector;
    pthread_create(&connector, NULL, run, NULL);
    printf("Main end...\n");
    return 0;
}

The output from the eclipse compiler:

**** Build of configuration Release for project ThreadTest ****

make all 
Building file: ../src/ThreadTest.cpp
Invoking: GCC C++ Compiler
mipsel-linux-g++ -O3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/ThreadTest.d" -MT"src/ThreadTest.d" -o"src/ThreadTest.o" "../src/ThreadTest.cpp" -lpthread
mipsel-linux-g++: -lpthread: linker input file unused because linking not done
Finished building: ../src/ThreadTest.cpp

Building target: ThreadTest
Invoking: GCC C++ Linker
mipsel-linux-g++  -o"ThreadTest"  ./src/ThreadTest.o    -lpthread -static
Finished building target: ThreadTest

Edit: Removed the old code and put in a new simpler example. This code runs if I compile it as a C program but no if I compile it as a c++ program. I´m runnig the 2.6.26.3 kernel on the board.

A: 

A correct declaration of main() is int main(int argc, char **argv)

edited to correct this answer
This is because your compile -c line for your .c include -lpthread:
linker input file unused

I found this answer about compiling c++ programs on openwrt:

http://manoftoday.wordpress.com/2007/10/11/writing-and-compiling-a-simple-program-for-openwrt/

I think you'll also want to read this to get gdb working:

http://forum.openwrt.org/viewtopic.php?pid=29712

codeDr
I tried both versions and neither work
Stulli
+1  A: 

This could easially be due to a low memory condition. You should try to enable some form of page file and free up any otyher memory.

Also, why -static? if your using a dynamic -lpthread, wouldnt linking the shared library be perferrable?

Also, it could be due to your C++ lib being mis-matched, make sure your uclibc++ is the correct version, you may also want to install ldd if you have not allready. Depends on your firmware.

RandomNickName42
Yea why -static?? I removed the flag and everything works fine.
Stulli
And a big thanks to everybody for their time and effort
Stulli
A: 

It's not sufficient to simple link against pthread with -lpthread. You need gcc -pthread (as an option its own right) or gcc -D_REENTRANT -lpthread (define a symbol named _REENTRANT). I don't know if this necessary affects anything.

A: 

I don't know if you found an answer yet or if this was the problem, but there is a race condition in the code you showed. It is possible that main will return and your program will try to exit before your "run" thread has finished running. You can never assume that it will run in any particular order or with any particular timing. You should add a call to pthread_join(connector, NULL); before returning from main.

Adam Kemp