无法使用 GCC 编译器 4.7.3 在 AIX 上编译具有线程支持的 c++ 程序

Could not compile c++ program with threads support on AIX with GCC compiler 4.7.3

本文关键字:编译 线程 支持 程序 c++ AIX GCC 编译器      更新时间:2023-10-16
在使用

gcc 编译器(版本 4.7.3)的 aix 机器上编译以下代码时出现问题:

SomeThread.h

#ifndef SomeThread_H
#define SomeThread_H
class SomeThread {
   public:
      SomeThread(void);
      virtual ~SomeThread(void);
      void runThread();
};    // SomeThread
#endif // _SomeThread_H_

某线程.cpp

#include "SomeThread.h"
#include <thread>
#include <iostream>

using namespace std;
namespace {
void foo_thread_function() {
   for (int i = 0; i < 10; ++i) {
      cout << "Some threaded text" << endl;
   }
}
}
SomeThread::SomeThread() {
}    // SomeThread
SomeThread::~SomeThread() {
}    // ~SomeThread

void SomeThread::runThread() {
   thread foo_thread_01(foo_thread_function);
   thread foo_thread_02(foo_thread_function);
   thread foo_thread_03(foo_thread_function);
   foo_thread_01.join();
   foo_thread_02.join();
   foo_thread_03.join();
}

我得到的错误如下:

SomeThread.cpp: In member function 'void SomeThread::runThread()':
SomeThread.cpp:58:4: error: reference to 'thread' is ambiguous
In file included from /usr/include/sys/ptrace.h:28:0,
                 from /usr/include/sys/proc.h:42,
                 from /usr/include/sys/pri.h:43,
                 from /usr/include/sys/sched.h:38,
                 from /usr/include/sched.h:51,
                 from /opt/freeware/lib/gcc/powerpc-ibm-aix7.1.0.0/4.7.3/include-fixed/pthread.h:76,
                 from /opt/freeware/lib/gcc/powerpc-ibm-aix7.1.0.0/4.7.3/include/c++/powerpc-ibm-aix7.1.0.0/pthread/ppc64/bits/gthr-posix.h:41,
                 from /opt/freeware/lib/gcc/powerpc-ibm-aix7.1.0.0/4.7.3/include/c++/powerpc-ibm-aix7.1.0.0/pthread/ppc64/bits/gthr-default.h:30,
                 from /opt/freeware/lib/gcc/powerpc-ibm-aix7.1.0.0/4.7.3/include/c++/powerpc-ibm-aix7.1.0.0/pthread/ppc64/bits/gthr.h:150,
                 from /opt/freeware/lib/gcc/powerpc-ibm-aix7.1.0.0/4.7.3/include/c++/ext/atomicity.h:34,
                 from /opt/freeware/lib/gcc/powerpc-ibm-aix7.1.0.0/4.7.3/include/c++/memory:75,
                 from /opt/freeware/lib/gcc/powerpc-ibm-aix7.1.0.0/4.7.3/include/c++/thread:40,
                 from SomeThread.cpp:5:
/usr/include/sys/thread.h:105:8: error: candidates are: struct thread
In file included from SomeThread.cpp:5:0:
/opt/freeware/lib/gcc/powerpc-ibm-aix7.1.0.0/4.7.3/include/c++/thread:60:9: error:                 class std::thread
SomeThread.cpp:58:11: error: expected ';' before 'foo_thread_01'
SomeThread.cpp:59:4: error: reference to 'thread' is ambiguous
In file included from /usr/include/sys/ptrace.h:28:0,
                 from /usr/include/sys/proc.h:42,
                 from /usr/include/sys/pri.h:43,
                 from /usr/include/sys/sched.h:38,
                 from /usr/include/sched.h:51,
                 from /opt/freeware/lib/gcc/powerpc-ibm-aix7.1.0.0/4.7.3/include-fixed/pthread.h:76,
                 from /opt/freeware/lib/gcc/powerpc-ibm-aix7.1.0.0/4.7.3/include/c++/powerpc-ibm-aix7.1.0.0/pthread/ppc64/bits/gthr-posix.h:41,
                 from /opt/freeware/lib/gcc/powerpc-ibm-aix7.1.0.0/4.7.3/include/c++/powerpc-ibm-aix7.1.0.0/pthread/ppc64/bits/gthr-default.h:30,
                 from /opt/freeware/lib/gcc/powerpc-ibm-aix7.1.0.0/4.7.3/include/c++/powerpc-ibm-aix7.1.0.0/pthread/ppc64/bits/gthr.h:150,
                 from /opt/freeware/lib/gcc/powerpc-ibm-aix7.1.0.0/4.7.3/include/c++/ext/atomicity.h:34,
                 from /opt/freeware/lib/gcc/powerpc-ibm-aix7.1.0.0/4.7.3/include/c++/memory:75,
                 from /opt/freeware/lib/gcc/powerpc-ibm-aix7.1.0.0/4.7.3/include/c++/thread:40,
                 from SomeThread.cpp:5:

我使用以下命令行编译上述文件:

g++ -maix64 -DTARGET=target_thread -DGENDATE=04_01_2017 -DTT_LIB_DLLSUFFIX=".so" -DOSNAME="AIX" -D_GNU_SOURCE -D_REENTRANT -DAIX -Wno-deprecated -I.  -std=gnu++11   -maix64 -pthread   -mminimal-toc  -fpermissive -Wno-write-strings -Winvalid-offsetof   -O3  -c -oSomeThread.o SomeThread.cpp

问题是,您的编译器选项和包含有多个thread实现。也许将代码更正为此就足够了。某线程.cpp

#include "SomeThread.h"
#include <thread>
#include <iostream>
//Stop using namespace std, please
namespace SomeNamespace {
void foo_thread_function() {
   for (int i = 0; i < 10; ++i) {
      cout << "Some threaded text" << endl;
   }
}
}
SomeThread::SomeThread() {
}    // SomeThread
SomeThread::~SomeThread() {
}    // ~SomeThread

void SomeThread::runThread() {
   std::thread foo_thread_01(SomeNamespace::foo_thread_function);
   std::thread foo_thread_02(SomeNamespace::foo_thread_function);
   std::thread foo_thread_03(SomeNamespace::foo_thread_function);
   foo_thread_01.join();
   foo_thread_02.join();
   foo_thread_03.join();
}  

模棱两可意味着同一个词有多种解释。例:

namespace Bla{
   struct SomeStruct{
   }
}
namespace Blub{
   struct SomeStruct{
   }
}
int main(){
   using namespace Bla;
   using namespace Blub;
   SomeStruct ImAmbiguous; // Problem now, which struct should the compiler choose now?
   Bla::SomeStruct structFromBla; //Now the compiler knows which struct should be choosen
   return 0;
}