编译 C++ 对 Openwrt Linux 使用 -pthreads - 获取分段错误
我对 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 内核。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这很可能是由于内存不足造成的。 您应该尝试启用某种形式的页面文件并释放任何其他内存。
另外,为什么是-static? 如果您使用动态
-lpthread
,链接共享库不是更好吗?另外,这可能是由于您的 C++ 库不匹配,请确保您的 uclibc++ 是正确的版本,如果您还没有安装 ldd,您可能还需要安装。 取决于你的固件。
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.使用 -lpthread 简单链接到 pthread 是不够的。 您需要 gcc -pthread (作为其本身的选项)或 gcc -D_REENTRANT -lpthread (定义名为 _REENTRANT 的符号)。 我不知道这是否有必要影响任何事情。
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.
我不知道您是否找到了答案,或者这是否是问题所在,但您显示的代码中存在竞争条件。 main 可能会返回,并且您的程序将在“运行”线程完成运行之前尝试退出。 您永远不能假设它将按任何特定顺序或任何特定时间运行。 您应该添加对 pthread_join(connector, NULL); 的调用 从 main 返回之前。
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.
在从主程序返回并退出程序之前,您应该执行
避免在线程终止之前退出应用程序的操作。
Before returning from the main and thus exiting the program, you should be doing a
which avoids exiting your application before the thread has terminated.
main()
的正确声明已编辑以更正此答案:
这是因为您的 .c 的编译 -c 行包括 -lpthread: 链接器输入文件未使用
我发现关于在 openwrt 上编译 C++ 程序的答案:
http://manoftoday.wordpress.com/2007/10/11/writing-and-compiling-a-simple-program-for-openwrt/
我想你也想读这个让 gdb 工作:
http://forum.openwrt.org/viewtopic.php?pid =29712
A correct declaration of
main()
isEdited 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