c++ - 为什么我的程序在调用 main 之前就退出了?

标签 c++ boost

当我在嵌入式 Linux 上执行以下程序时,没有任何反应:

#include <boost/thread/thread.hpp>
#include <boost/lockfree/spsc_queue.hpp>
#include <iostream>

#include <boost/atomic.hpp>

void Test(void)
{
    std::cout << "Hello World" << std::endl;
}

int main(int argc, char* argv[])
{
    std::cout << "init";

    boost::thread producer_thread(Test);

    producer_thread.join();

    std::cout << "end";
}


# ./prog -> nothing happens here

strace 输出的最后几行是:

open("/lib/libboost_thread.so.1.55.0", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0(\0\1\0\0\0\240\272\0\0004\0\0\0"..., 512) = 512
lseek(3, 95536, SEEK_SET)               = 95536
read(3, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 1200) = 1200
lseek(3, 95226, SEEK_SET)               = 95226
read(3, "A'\0\0\0aeabi\0\1\35\0\0\0\0055T\0\6\3\10\1\t\1\22\4\24\1\25\1"..., 40) = 40
exit_group(1)                           = ?
+++ exited with 1 +++
# 

交叉编译的libbost_thread正确安装在/lib。

程序在调用 main() 之前退出。程序在我的Ubuntu下运行正常。

目标:带有 buildroot 的 ARM (sama5d3)

工具链:arm-linux-gnueabihf-

问候

最佳答案

也许作为一个提示:

您是否已使用目标的编译和链接选项 -pthread 链接到 libpthread?

如果没有,它可能会产生与您的环境中看到的相同的效果:prog 启动,尝试启动一个新线程,不启用线程并调用 abort() 函数。因为 abort() 只是让程序在退出代码中出现错误,不会发生任何其他事情。

您是否还可以添加用于调试目的的编译和链接命令!

此外:

没有 endl 的输出将不会被打印,因为 cout 已被缓冲。仅当您调用 flush 或发送 endl 时才会打印缓冲区。也许你在你的例子中改变了这一点。

希望有帮助...

关于c++ - 为什么我的程序在调用 main 之前就退出了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28696352/

相关文章:

c++ - 在类实例化期间未设置继承值

c++ - 从 recursive_directory_iterator 中获取字符串

C++ 错误 : expected ‘,’ or ‘...’ before ‘distances’

c++ - boost::posix_time::ptime 中的特定时区

c++ - 如何使用 boost::bind 创建函数包装器

c++ - 发生哪些线程异步操作

c++ - 无法推断 'T' 的模板参数

windows - 在 Windows "#error "上构建 boost 时出错 Not ARM ""& "错误 : No best alternative for"

c++ - 使用英特尔编译器 12 和 Visual Studio 2010 编译 boost 1_46_1 时出现问题

c++ - QVector 总是推回 0?