Stulli
Stulli

Reputation: 177

Compiling C++ using -pthreads for Openwrt Linux-Get segmentation fault

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.

Upvotes: 2

Views: 7749

Answers (5)

RandomNickName42
RandomNickName42

Reputation: 5965

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

Also, why -static? if your using a dynamic -lpthread, wouldn't linking the shared library be preferable?

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 already. Depends on your firmware.

Upvotes: 1

codeDr
codeDr

Reputation: 1694

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

Upvotes: 0

Atmocreations
Atmocreations

Reputation: 10071

Before returning from the main and thus exiting the program, you should be doing a

pthread_join(connector, NULL);

which avoids exiting your application before the thread has terminated.

Upvotes: 0

Adam Kemp
Adam Kemp

Reputation: 11

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.

Upvotes: 0

Some One
Some One

Reputation:

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.

Upvotes: 0

Related Questions