【问题标题】:C++ Compile error on NetBSD '::system' has not been declaredNetBSD '::system' 上的 C++ 编译错误尚未声明
【发布时间】:2015-06-20 09:39:06
【问题描述】:

我有一些 C++ 代码可以在 Linux 上很好地编译,但到目前为止我无法让它在 NetBSD 上正确编译。

这些是我的包括:

#include <fstream>
#include <iostream>
#include <cstdlib>
#include <unistd.h>
#include <iomanip>
#include <boost/regex.hpp>

我用这个命令编译:

g++ -v -O2 -fno-strict-aliasing -I /usr/pkg/include \
barefoot.cpp -o barefoot -L /usr/pkg/lib/ -lboost_regex \
-L /usr/pkg/lib/ -lboost_regex -lpthreads

渲染这个输出:

Using built-in specs.
COLLECT_GCC=g++
Target: x86_64--netbsd

Configured with: /usr/src2/tools/gcc/../../external/gpl3/gcc/dist/configure --target=x86_64--netbsd --enable-long-long --enable-threads --with-bugurl=http://www.NetBSD.org/Misc/send-pr.html --with-pkgversion='NetBSD nb2 20111202' --enable-__cxa_atexit --with-tune=nocona --with-mpc=/var/obj/mknative/amd64/usr/src2/destdir.amd64/usr --with-mpfr=/var/obj/mknative/amd64/usr/src2/destdir.amd64/usr --with-gmp=/var/obj/mknative/amd64/usr/src2/destdir.amd64/usr --enable-tls --disable-multilib --disable-symvers --disable-libstdcxx-pch --build=x86_64-unknown-netbsd5.99.56 --host=x86_64--netbsd

Thread model: posix

gcc version 4.5.3 (NetBSD nb2 20110806) 
COLLECT_GCC_OPTIONS='-v' '-O2' '-fno-strict-aliasing' '-I' '/usr/pkg/include/' '-o' 'a.out' '-L' '/usr/pkg/lib/' '-mtune=nocona' '-march=x86-64'
 /usr/libexec/cc1plus -quiet -v -I /usr/pkg/include/ barefoot.cpp -quiet -dumpbase barefoot.cpp -mtune=nocona -march=x86-64 -auxbase barefoot -O2 -version -fno-strict-aliasing -o /var/tmp//cc9Dcmwi.s
GNU C++ (NetBSD nb2 20110806) version 4.5.3 (x86_64--netbsd)
        compiled by GNU C version 4.5.3, GMP version 5.0.2, MPFR version 3.0.1-p4, MPC version 0.9

GGC heuristics: --param ggc-min-expand=99 --param ggc-min-heapsize=131007
#include "..." search starts here:
#include <...> search starts here:
 /usr/pkg/include/
 /usr/include/g++
 /usr/include/g++/backward
 /usr/include/gcc-4.5
 /usr/include
End of search list.

GNU C++ (NetBSD nb2 20110806) version 4.5.3 (x86_64--netbsd)
        compiled by GNU C version 4.5.3, GMP version 5.0.2, MPFR version 3.0.1-p4, MPC version 0.9
GGC heuristics: --param ggc-min-expand=99 --param ggc-min-heapsize=131007
Compiler executable checksum: a34e7d170f4dd8d4687d2b62e8dca4b7
In file included from /usr/include/g++/bits/gthr.h:166:0,
                 from /usr/include/g++/ext/atomicity.h:34,
                 from /usr/include/g++/bits/ios_base.h:41,
                 from /usr/include/g++/ios:43,
                 from /usr/include/g++/istream:40,
                 from /usr/include/g++/fstream:40,
                 from barefoot.cpp:29:
/usr/include/g++/bits/gthr-default.h:130:9: error: 'pthread_detach' was not declared in this scope
/usr/include/g++/bits/gthr-default.h:130:1: error: invalid type in declaration before ';' token
/usr/include/g++/bits/gthr-default.h: In function 'int __gthread_detach(pthread_st*)':
/usr/include/g++/bits/gthr-default.h:668:46: error: '__gthrw_pthread_detach' cannot be used as a function
In file included from /usr/pkg/include/boost/regex/v4/regex_workaround.hpp:25:0,
                 from /usr/pkg/include/boost/regex/v4/regex.hpp:32,
                 from /usr/pkg/include/boost/regex.hpp:31,
                 from barefoot.cpp:32:
/usr/include/g++/cstdlib: At global scope:
/usr/include/g++/cstdlib:132:11: error: '::system' has not been declared

我尝试添加-lpthread,但结果相同。不知道接下来要尝试什么。

【问题讨论】:

  • 看起来你忘了#include 一个必需的标题,例如&lt;cstdlib&gt;&lt;unistd.h&gt; ?
  • 谢谢。我添加了 unistd.h 但没有变化。完全相同的输出。
  • 尝试添加-pthread,而不是-lpthread
  • -pthread 呈现同样的错误。
  • 第一个错误指出pthread_detach 丢失。找出您的系统包含哪个pthread.h 文件,然后检查pthread_detach 是否在其中。然后,检查是什么#if 守卫阻止它被看到。然后检查为什么您的系统定义了阻止其可见的宏。

标签: c++ unix boost g++ netbsd


【解决方案1】:

我不知道您遇到此问题的确切原因,但我可以为您提供一些后续步骤来尝试。

  • 请注意,第一个错误是缺少pthread_detach。系统头文件通常会确保包含他们需要的任何头文件,但您可以尝试在所有其他头文件上方添加&lt;pthread.h&gt;

  • 1234563 >
  • 假设它在那里,一定有一些条件编译导致它对您的源代码不可见。找出条件守卫,以及影响它的宏值。

  • 然后,您需要了解为什么您的系统定义宏值的方式会导致函数 pthread_detach 不可见。

通过此调查,您可能会发现您没有包含正确的&lt;pthread.h&gt; 文件。这可能是由于系统包含目录与您要使用的包含目录冲突。

This answer(它位于底部)到this question 显示如何显示g++ 编译器的预定义宏。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-22
    • 2013-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多