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

354 views Asked by At

Here is my cmakelist.txt, and I am trying to use g++ as compiler to do multithreading.

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})

When I just use g++ -pthread myproject.cpp without ITK library, it works fine, but when I am trying to use itk library and thread at the same time and make a cmakelist.txt as above, it can be compiled but when I make it, error pop up as following.

/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 

and when I use default compiler (clang) to compile and make ITK, it works fine, whereas there will be error in thread part

[ 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

So I am wondering, did I do something wrong? or how can I use g++ to link itk library? Appreciated for any help

1

There are 1 answers

4
Dženan On BEST ANSWER

Clang's message is useful here:

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

Try replacing that array by std::vector

std::thread t[numthreads]; //won't compile
std::vector<std::thread> t(numthreads); //might compile