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

时间:2017-01-05 16:34:58

标签: c++ multithreading gcc aix

使用gcc编译器(版本4.7.3)在aix机器上编译下面的代码时遇到问题:

SomeThread.h

#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

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

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

   @media print{
       @page{ 

       size: 4in 6in;
       margin:auto;
       margin-left: 0px;
       margin-right: 0px;
       margin-top: 0px;
       margin-bottom: 5px;
       padding-top:10px;  
}
}

1 个答案:

答案 0 :(得分:2)

问题是,thread有多个带有编译器选项和包含的实现。也许只需要纠正代码即可。 SomeThread.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;
}