首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法用GCC编译器4.7.3在AIX上编译线程支持的c++程序

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

Stack Overflow用户
提问于 2017-01-05 16:34:58
回答 1查看 1.2K关注 0票数 0

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

SomeThread.h

代码语言:javascript
复制
#ifndef SomeThread_H
#define SomeThread_H

class SomeThread {

   public:

      SomeThread(void);

      virtual ~SomeThread(void);

      void runThread();

};    // SomeThread

#endif // _SomeThread_H_

SomeThread.cpp

代码语言:javascript
复制
#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();
}

我得到的错误如下:

代码语言:javascript
复制
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:

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

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-01-05 16:41:54

问题是,有多个thread实现与您的编译器选项和包括。也许这就足以纠正这段代码了。SomeThread.cpp

代码语言:javascript
复制
#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();
}  

歧义意味着对同一个词有多种解释。示例:

代码语言:javascript
复制
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;
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41490085

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档