我对 C++ 编程很陌生,我正在使用 pthreads。我正在为 OpenWRT 交叉编译我的代码,但由于某种原因,当我在我的板上运行程序时出现分段错误,但它在我的 PC 上运行良好。我怀疑错误发生在编译的链接阶段,因为我尝试了一个小的 C 程序并且效果很好。此外,如果我将文件名更改为 .cpp 并使用 g++ 编译它也可以工作。
#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;
}
eclipse编译器的输出:
**** 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
编辑:删除旧代码并放入一个新的更简单的示例。如果我将其编译为 C 程序,则此代码会运行,但如果我将其编译为 C++ 程序则不会。我在板上运行 2.6.26.3 内核。