【问题标题】:Could not compile c++ program with threads support on AIX with GCC compiler 4.7.3无法使用 GCC 编译器 4.7.3 在 AIX 上编译具有线程支持的 c++ 程序
【发布时间】:2017-05-20 07:04:01
【问题描述】:

在使用 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_

SomeThread.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

【问题讨论】:

  • 可以肯定,对于 AIX 7.1,fixinclude 不是最新的。你需要 gcc 4.8.3-1。 perzl.org/aix/index.php?n=Main.Gcc
  • @stark:在 Linux 上,我们使用的是 gcc 版本 4.7.1,一切正常。非常奇怪的行为。
  • 也不适用于 g++-4.8.3 (AIX-6.1)(如果您想要一个工作程序,您可能希望使用 C 语言和 pthreads)
  • @LorinczyZsigmond:解决方案是写std::thread 而不是thread

标签: c++ multithreading gcc aix


【解决方案1】:

问题是,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;
}

【讨论】:

  • 未命名的命名空间完全有效。
  • 哦,是的,对不起。这完全没用xD
  • ""//请停止使用命名空间std" 为什么?它不在标题中。
  • 只是不喜欢它,对不起。我更喜欢它只用于小实体,因为它可能会导致模棱两可的错误。
  • 我编写的代码只是为了证明我也可以在 AIX 上使用线程的概念 :)。我知道写得不太好,但我学到了一些新东西,所以我以后会用它们:)。
猜你喜欢
  • 1970-01-01
  • 2017-07-07
  • 1970-01-01
  • 1970-01-01
  • 2011-03-11
  • 2012-04-05
  • 1970-01-01
  • 2015-05-17
  • 1970-01-01
相关资源
最近更新 更多