Cmake:如何将 g++ 用于 link itk 或其他库

Cmake: How to use g++ to link itk or other libraries

这是我的 cmakelist.txt,我正在尝试使用 g++ 作为编译器来进行多线程处理。

cmake_minimum_required(VERSION 3.7)

project(myproject)

# Find ITK.
find_package(ITK REQUIRED)
include(${ITK_USE_FILE})

find_package(Threads REQUIRED)

add_executable(myproject myproject.cxx )

target_link_libraries(myproject ${ITK_LIBRARIES})
target_link_libraries(myproject ${CMAKE_THREAD_LIBS_INIT})

当我只使用 g++ -pthread myproject.cpp 而没有 ITK 库时,它工作正常,但是当我尝试同时使用 itk 库和线程并像上面那样制作 cmakelist.txt 时,它可以可以编译但是当我编译时,错误弹出如下。

/var/folders/d_/_8wsw0xn04762dyvlw30mk8nn993l7/T//cc6d3Cv9.s:80661:11: warning: section "__const_coal" is deprecated
        .section __TEXT,__const_coal,coalesced
                 ^      ~~~~~~~~~~~~
/var/folders/d_/_8wsw0xn04762dyvlw30mk8nn993l7/T//cc6d3Cv9.s:80661:11: note: change section name to "__const"
        .section __TEXT,__const_coal,coalesced
                 ^      ~~~~~~~~~~~~
[100%] Linking CXX executable myproject
Undefined symbols for architecture x86_64:
  "itk::ImageIOBase::GetComponentTypeAsString[abi:cxx11](itk::ImageIOBase::IOComponentType)", referenced from:
      itk::ImageFileReader<itk::Image<unsigned short, 3u>, itk::DefaultConvertPixelTraits<unsigned short> >::GenerateData() in Heterogeneity3D.cxx.o
      itk::ImageFileReader<itk::Image<unsigned short, 3u>, itk::DefaultConvertPixelTraits<unsigned short> >::DoConvertBuffer(void*, unsigned long) in Heterogeneity3D.cxx.o

         construction vtable for std::__1::basic_iostream<char, std::__1::char_traits<char> >-in-std::__1::basic_stringstream<char, std::__1::char_traits<char>, std::__1::allocator<char> > in libITKCommon-4.11.a(itkFloatingPointExceptions.cxx.o)
......
      construction vtable for std::__1::basic_iostream<char, std::__1::char_traits<char> >-in-std::__1::basic_stringstream<char, std::__1::char_traits<char>, std::__1::allocator<char> > in libitkgdcmMSFF-4.11.a(gdcmImageHelper.cxx.o)
      construction vtable for std::__1::basic_iostream<char, std::__1::char_traits<char> >-in-std::__1::basic_stringstream<char, std::__1::char_traits<char>, std::__1::allocator<char> > in libitkgdcmMSFF-4.11.a(gdcmImageReader.cxx.o)
      construction vtable for std::__1::basic_iostream<char, std::__1::char_traits<char> >-in-std::__1::basic_stringstream<char, std::__1::char_traits<char>, std::__1::allocator<char> > in libitkgdcmMSFF-4.11.a(gdcmStringFilter.cxx.o)
      construction vtable for std::__1::basic_iostream<char, std::__1::char_traits<char> >-in-std::__1::basic_stringstream<char, std::__1::char_traits<char>, std::__1::allocator<char> > in libitkgdcmMSFF-4.11.a(gdcmDataSetHelper.cxx.o)
      ...
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
make[2]: *** [myproject] Error 1
make[1]: *** [CMakeFiles/myproject.dir/all] Error 2
make: *** [all] Error 2 

当我使用默认编译器(clang)编译制作ITK时,它工作正常,而线程部分会出错

[ 50%] Building CXX object CMakeFiles/myproject.dir/myproject.cxx.o
/Users/miaot2/c++/myproject/myproject.cxx:465:18: error: 
      variable length array of non-POD element type 'std::thread'
    std::thread t[numthreads];
                 ^
/Users/miaot2/c++/myproject/myproject.cxx:470:12: error: no
      matching constructor for initialization of 'std::thread'
      t[i]=std::thread(calculateAll,data,indexList,s,e);
           ^           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/thread:389:9: note: 
      candidate constructor template not viable: requires single argument '__f',
      but 5 arguments were provided
thread::thread(_Fp __f)
        ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/thread:297:5: note: 
      candidate constructor not viable: requires 1 argument, but 5 were provided
    thread(const thread&);
    ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/thread:304:5: note: 
      candidate constructor not viable: requires 0 arguments, but 5 were
      provided
    thread() _NOEXCEPT : __t_(0) {}
    ^
2 errors generated.
make[2]: *** [CMakeFiles/myproject.dir/myproject.cxx.o] Error 1
make[1]: *** [CMakeFiles/myproject.dir/all] Error 2
make: *** [all] Error 2

所以我想知道,我做错了什么吗?或者如何使用 g++ 到 link itk 库?感谢您的帮助

Clang 的消息在这里很有用:

variable length array of non-POD element type 'std::thread'

尝试用 std::vector

替换该数组
std::thread t[numthreads]; //won't compile
std::vector<std::thread> t(numthreads); //might compile