I'm相当新的C编程++和I'm使用并行线程。 I'm交叉编译我的的OpenWRT,但由于某种原因,我得到段错误代码,当我在主板上运行的程序,但它运行在我的电脑罚款。我怀疑,在编译的链接阶段,因为我试过小的C程序和工作正常发生错误。另外,如果我更改文件的名称。cpp和。用克编译它++它也适用。

#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 ++程序运行。 I'm运行过程中出现在板的2.6.26.3内核。

有帮助吗?

解决方案

此可以很容易地由于低存储器状态。你应该尝试启用某种形式的页面文件和释放任何其他存储器。

另外,为什么-static?如果使用动态-lpthread,不会链接共享库是优选?

此外,它可能是由于你的C ++的lib存在不匹配,请确保您的uclibc++是正确的版本,你可能也想,如果你还没有安装LDD。取决于你的固件。

其他提示

这不足以对抗并行线程简单的链接与-lpthread。你需要GCC -pthread(作为一种选择自己的权利)或GCC -D_REENTRANT -lpthread(定义符号命名_REENTRANT)。我不知道这是否有必要将影响任何东西。

我不知道你是否找到了答案又或者,如果这是问题,但在你表现出码的竞争条件。这可能是主要将返回和你的程序将尝试退出你的“运行”线程完成运行前。你永远不能认为它会在任何特定的顺序或任何特定时间运行。您应该添加调用在pthread_join(连接器,NULL);之前,从主返回。

从主返回,从而在退出程序之前,应该做的一个

pthread_join(connector, NULL);

,其避免退出你的应用程序的线程终止之前。

main()的正确声明是

int main(int argc, char **argv)

<强>编辑,以更正此答案:

这是因为你的.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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top