How to link Boost library with CMake (on a cluster if boost is in non-standard location)? - c++

I have a strange problem, when I try to build a simple Boost test program on a cluster.
On my machine, everything works just fine.
First the example:
//main.cpp
#include <iostream>
#include <string>
#include "boost/program_options.hpp"
namespace po = boost::program_options;
int main( int argc , char* argv[] ) {
po::options_description desc("Allowed options");
desc.add_options()
("help", "produce help message")
("greet", po::value<std::string>()->default_value("World"), "the greeting")
;
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
std::cout << "Hello, " << vm["greet"].as<std::string>() << "!" << std::endl;
return 0;
}
And the CMakeLists.txt
cmake_minimum_required(VERSION 3.5)
project(boost_test)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
find_package(Boost COMPONENTS program_options)
set(SOURCE_FILES main.cpp)
if(Boost_FOUND)
add_executable(boost_test ${SOURCE_FILES})
target_include_directories(boost_test PRIVATE ${Boost_INCLUDE_DIRS})
target_link_libraries(boost_test PRIVATE ${Boost_LIBRARIES})
endif(Boost_FOUND)
I use cmake like
cmake -G "Unix Makefiles" /path/to/code
make
The error I get is a undefined reference error during linking:
Linking CXX executable boost_test
CMakeFiles/boost_test.dir/main.cpp.o: In function `main':
main.cpp:(.text+0x6d): undefined reference to `boost::program_options::options_description::options_description(std::string const&, unsigned int, unsigned int)'
CMakeFiles/boost_test.dir/main.cpp.o: In function `boost::program_options::variables_map::operator[](std::string const&) const':
main.cpp:(.text._ZNK5boost15program_options13variables_mapixERKSs[_ZNK5boost15program_options13variables_mapixERKSs]+0x1f): undefined reference to `boost::program_options::abstract_variables_map::operator[](std::string const&) const'
and so on ...
So I thought that maybe the installation of boost is not correct and I tried to write a makefile myself.
boost-test-1: ../code/main.cpp
g++ ../code/main.cpp -o boost-test-1 -lboost_program_options
boost-test-2: ../code/main.cpp
g++ ../code/main.cpp -o boost-test-2 /some/fancy/cluster/path/certainly/non/standard/Boost/lib/libboost_program_options.so
Both ways to build the program work fine. But what cmake is doing is basically the following: first, build a object file, second link the object file and the library together and that fails.
boost-test-3: ../code/main.cpp
/usr/bin/c++ -I/some/fancy/cluster/path/certainly/non/standard/Boost/include -std=c++11 -o main.cpp.o -c ../code/main.cpp
/usr/bin/c++ -std=c++11 main.cpp.o -o boost-test-3 /some/fancy/cluster/path/certainly/non/standard/Boost/lib/libboost_program_options.so
As already mentioned, on my computer all described ways to build the program work, but on the cluster, the cmake way unfortunately fails. The problem is that I have another larger project that uses cmake and I am looking for a way to build it on that particular cluster.
Do you have any idea what could cause the problem and how to solve it?
Thank you!
Update: This is the whole output of the build process. I shortened the paths to cmake and boost to "path/to/cmake" and "path/to/boost" to make it shorter and better readable, but it is the same long path as described above.
$ cmake -G "Unix Makefiles" ../code/
-- The C compiler identification is GNU 4.8.5
-- The CXX compiler identification is GNU 4.8.5
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Boost version: 1.61.0
-- Found the following Boost libraries:
-- program_options
-- Boost include directories (Boost_INCLUDE_DIRS): /path/to/Boost/include
-- Link directories for Boost libraries (Boost_LIBRARY_DIRS): /path/to/Boost/lib
-- Boost component libraries to be linked (Boost_LIBRARIES): /path/to/Boost/lib/libboost_program_options.so
-- Configuring done
-- Generating done
-- Build files have been written to: /home/test/build
$ make VERBOSE=1
/path/to/CMake/bin/cmake -H/home/test/code -B/home/test/build --check-build-system CMakeFiles/Makefile.cmake 0
/path/to/CMake/bin/cmake -E cmake_progress_start /home/test/build/CMakeFiles /home/test/build/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory `/home/test/build'
make -f CMakeFiles/boost_test.dir/build.make CMakeFiles/boost_test.dir/depend
make[2]: Entering directory `/home/test/build'
cd /home/test/build && /path/to/CMake/bin/cmake -E cmake_depends "Unix Makefiles" /home/test/code /home/test/code /home/test/build /home/test/build /home/test/build/CMakeFiles/boost_test.dir/DependInfo.cmake --color=
Dependee "/home/test/build/CMakeFiles/boost_test.dir/DependInfo.cmake" is newer than depender "/home/test/build/CMakeFiles/boost_test.dir/depend.internal".
Dependee "/home/test/build/CMakeFiles/CMakeDirectoryInformation.cmake" is newer than depender "/home/test/build/CMakeFiles/boost_test.dir/depend.internal".
Scanning dependencies of target boost_test
make[2]: Leaving directory `/home/test/build'
make -f CMakeFiles/boost_test.dir/build.make CMakeFiles/boost_test.dir/build
make[2]: Entering directory `/home/test/build'
[ 50%] Building CXX object CMakeFiles/boost_test.dir/main.cpp.o
/usr/bin/c++ -I/path/to/Boost/include -std=c++11 -o CMakeFiles/boost_test.dir/main.cpp.o -c /home/test/code/main.cpp
[100%] Linking CXX executable boost_test
/path/to/CMake/bin/cmake -E cmake_link_script CMakeFiles/boost_test.dir/link.txt --verbose=1
/usr/bin/c++ -std=c++11 CMakeFiles/boost_test.dir/main.cpp.o -o boost_test /path/to/Boost/lib/libboost_program_options.so
CMakeFiles/boost_test.dir/main.cpp.o: In function `main':
main.cpp:(.text+0x6d): undefined reference to `boost::program_options::options_description::options_description(std::string const&, unsigned int, unsigned int)'
CMakeFiles/boost_test.dir/main.cpp.o: In function `boost::program_options::variables_map::operator[](std::string const&) const':
main.cpp:(.text._ZNK5boost15program_options13variables_mapixERKSs[_ZNK5boost15program_options13variables_mapixERKSs]+0x1f): undefined reference to `boost::program_options::abstract_variables_map::operator[](std::string const&) const'
CMakeFiles/boost_test.dir/main.cpp.o: In function `boost::program_options::basic_command_line_parser<char>::basic_command_line_parser(int, char const* const*)':
main.cpp:(.text._ZN5boost15program_options25basic_command_line_parserIcEC2EiPKPKc[_ZN5boost15program_options25basic_command_line_parserIcEC5EiPKPKc]+0x76): undefined reference to `boost::program_options::detail::cmdline::cmdline(std::vector<std::string, std::allocator<std::string> > const&)'
CMakeFiles/boost_test.dir/main.cpp.o: In function `boost::program_options::basic_command_line_parser<char>::extra_parser(boost::function1<std::pair<std::string, std::string>, std::string const&>)':
main.cpp:(.text._ZN5boost15program_options25basic_command_line_parserIcE12extra_parserENS_9function1ISt4pairISsSsERKSsEE[_ZN5boost15program_options25basic_command_line_parserIcE12extra_parserENS_9function1ISt4pairISsSsERKSsEE]+0x33): undefined reference to `boost::program_options::detail::cmdline::set_additional_parser(boost::function1<std::pair<std::string, std::string>, std::string const&>)'
CMakeFiles/boost_test.dir/main.cpp.o: In function `std::vector<std::string, std::allocator<std::string> > boost::program_options::to_internal<std::string>(std::vector<std::string, std::allocator<std::string> > const&)':
main.cpp:(.text._ZN5boost15program_options11to_internalISsEESt6vectorISsSaISsEERKS2_IT_SaIS5_EE[_ZN5boost15program_options11to_internalISsEESt6vectorISsSaISsEERKS2_IT_SaIS5_EE]+0x46): undefined reference to `boost::program_options::to_internal(std::string const&)'
CMakeFiles/boost_test.dir/main.cpp.o:(.rodata._ZTVN5boost15program_options11typed_valueISscEE[_ZTVN5boost15program_options11typed_valueISscEE]+0x40): undefined reference to `boost::program_options::value_semantic_codecvt_helper<char>::parse(boost::any&, std::vector<std::string, std::allocator<std::string> > const&, bool) const'
CMakeFiles/boost_test.dir/main.cpp.o: In function `boost::program_options::typed_value<std::string, char>::name() const':
main.cpp:(.text._ZNK5boost15program_options11typed_valueISscE4nameEv[_ZNK5boost15program_options11typed_valueISscE4nameEv]+0x32): undefined reference to `boost::program_options::arg'
CMakeFiles/boost_test.dir/main.cpp.o: In function `boost::program_options::typed_value<std::string, char>::xparse(boost::any&, std::vector<std::string, std::allocator<std::string> > const&) const':
main.cpp:(.text._ZNK5boost15program_options11typed_valueISscE6xparseERNS_3anyERKSt6vectorISsSaISsEE[_ZNK5boost15program_options11typed_valueISscE6xparseERNS_3anyERKSt6vectorISsSaISsEE]+0x7a): undefined reference to `boost::program_options::validate(boost::any&, std::vector<std::string, std::allocator<std::string> > const&, std::string*, int)'
collect2: error: ld returned 1 exit status
make[2]: *** [boost_test] Error 1
make[2]: Leaving directory `/home/test/build'
make[1]: *** [CMakeFiles/boost_test.dir/all] Error 2
make[1]: Leaving directory `/home/test/build'
make: *** [all] Error 2

Thank you Amadeus, Someprogrammerdude and Tsyvarev for leading me in the right direction here :)
In fact, it was a compiler issue: The default compiler on the cluster was gcc-4.8.5, which cannot handle the latest Boost versions. Therefore, we loaded a module for gcc-5.4 but /usr/bin/gcc was still gcc-4.8.5. When we set the cxx compiler for cmake to gcc-5.4 the build process still failed, since we also use CUDA and per default the host code is not forwarded to the cxx compiler but the c compiler, which was still gcc-4.8.5. So after setting CUDA_HOST_COMPILER to CMAKE_CXX_COMPILER it works fine now.

Related

CMake + MSys2 undefined references to everything (including c++ runtime)

I'm playing around with developing a cross-platform C++ project. Things build fine on Linux, but on Windows (10) + MSys2 I've run into a strange issue. Compile works fine (picks up my include dirs, etc.), but linking fails with all sorts of undefined reference errors to a static imported library I have, and even the C++ runtime.
I've tried setting CMAKE_C[XX]_COMPILER, CMAKE_MAKE_PROGRAM, but the output from the configuration step is always the same:
$ cmake ..
-- The C compiler identification is GNU 10.2.0
-- The CXX compiler identification is GNU 10.2.0
System is unknown to cmake, create:
Platform/MINGW64_NT-10.0-19041 to use this system, please post your config file on discourse.cmake.org so it can be added to cmake
-- Detecting C compiler ABI info
System is unknown to cmake, create:
Platform/MINGW64_NT-10.0-19041 to use this system, please post your config file on discourse.cmake.org so it can be added to cmake
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /mingw64/bin/cc.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
System is unknown to cmake, create:
Platform/MINGW64_NT-10.0-19041 to use this system, please post your config file on discourse.cmake.org so it can be added to cmake
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /mingw64/bin/CC.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: <....>
As mentioned earlier the compile works, but linking the executable fails spectacularly. Here is my minimal working example:
$ cat ../CMakeLists.txt
project(example)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)
add_executable(example
main.cpp
)
Here is an sample of the output (the rest is omitted for brevity):
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/example.dir/main.cpp.obj:main.cpp:(.text+0x51): undefined reference to `std::ios_base::Init::~Init()'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/example.dir/main.cpp.obj:main.cpp:(.text+0x81): undefined reference to `std::ios_base::Init::Init()'
Adding -v to cmake produces the following commands.
Compile:
/mingw64/bin/CC.exe -std=gnu++17 -o CMakeFiles/example.dir/main.cpp.obj -c /home/.../Development/minex/main.cpp
Link:
/mingw64/bin/CC.exe CMakeFiles/example.dir/main.cpp.obj -o example
CC.exe seems off... and it's used if I set the CXX compiler flag or not...
I also tried generating "MSYS2 Makefiles" but that also fails (doesn't know the generator).
I can reproduce the output by running
$ CC main.cpp -o example
while
$ g++ main.cpp -o example
works fine.
CMake version is 3.18.4.
Edit: This is the entire output of running make VERBOSE=1 (using mingw64-cmake seems to produce the same output, except the 'entering directory' and 'leaving directory' paths are absolute windows paths):
$ cat log
/usr/bin/cmake.exe -S/home/<...>/Development/minex -B/home/<...>/Development/minex/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake.exe -E cmake_progress_start /home/<...>/Development/minex/build/CMakeFiles /home/<...>/Development/minex/build//CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory '/home/<...>/Development/minex/build'
make -f CMakeFiles/example.dir/build.make CMakeFiles/example.dir/depend
make[2]: Entering directory '/home/<...>/Development/minex/build'
cd /home/<...>/Development/minex/build && /usr/bin/cmake.exe -E cmake_depends "Unix Makefiles" /home/<...>/Development/minex /home/<...>/Development/minex /home/<...>/Development/minex/build /home/<...>/Development/minex/build /home/<...>/Development/minex/build/CMakeFiles/example.dir/DependInfo.cmake --color=
Dependee "/home/<...>/Development/minex/build/CMakeFiles/example.dir/DependInfo.cmake" is newer than depender "/home/<...>/Development/minex/build/CMakeFiles/example.dir/depend.internal".
Dependee "/home/<...>/Development/minex/build/CMakeFiles/CMakeDirectoryInformation.cmake" is newer than depender "/home/<...>/Development/minex/build/CMakeFiles/example.dir/depend.internal".
Scanning dependencies of target example
make[2]: Leaving directory '/home/<...>/Development/minex/build'
make -f CMakeFiles/example.dir/build.make CMakeFiles/example.dir/build
make[2]: Entering directory '/home/<...>/Development/minex/build'
[ 50%] Building CXX object CMakeFiles/example.dir/main.obj
/mingw64/bin/CC.exe -std=gnu++17 -o CMakeFiles/example.dir/main.obj -c /home/<...>/Development/minex/main.cpp
[100%] Linking CXX executable example
/usr/bin/cmake.exe -E cmake_link_script CMakeFiles/example.dir/link.txt --verbose=1
/mingw64/bin/CC.exe CMakeFiles/example.dir/main.obj -o example
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/example.dir/main.obj:main.cpp:(.text+0x23): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/example.dir/main.obj:main.cpp:(.text+0x32): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/example.dir/main.obj:main.cpp:(.text+0x51): undefined reference to `std::ios_base::Init::~Init()'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/example.dir/main.obj:main.cpp:(.text+0x81): undefined reference to `std::ios_base::Init::Init()'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/example.dir/main.obj:main.cpp:(.rdata$.refptr._ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_[.refptr._ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_]+0x0): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/example.dir/main.obj:main.cpp:(.rdata$.refptr._ZSt4cout[.refptr._ZSt4cout]+0x0): undefined reference to `std::cout'
collect2.exe: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/example.dir/build.make:103: example] Error 1
make[2]: Leaving directory '/home/<...>/Development/minex/build'
make[1]: *** [CMakeFiles/Makefile2:95: CMakeFiles/example.dir/all] Error 2
make[1]: Leaving directory '/home/<...>/Development/minex/build'
make: *** [Makefile:103: all] Error 2
Solution:
I was setting CMAKE_CXX_COMPILER wrong :/. I was doing it from memory, and I just did
CMAKE_CXX_COMPILER=... cmake ..
not
cmake .. -DCMAKE_CXX_COMPILER=...
However! It's still weird that CC is used to successfully compile cpp files, but it can't link the object files.
If you are using mingw64 compiler in MSYS2 make sure you are using mingw64 version of cmake too.
Using cmake not aligned with gcc e.g.:
MINGW64
# which gcc
/mingw64/bin/gcc
MINGW64
# which cmake
/usr/bin/cmake
Will led to following error when running cmake:
...
-- The CXX compiler identification is GNU 10.2.0
System is unknown to cmake, create:
Platform/MINGW64_NT-10.0-19041 to use this system, please post your config file on discourse.cmake.org so it can be added to cmake
...
and linker error in build step.
So make sure you install mingw64 version of cmake:
MINGW64
pacman -S mingw-w64-x86_64-cmake
You need to close terminal and open it again after cmake is installed. Then make sure you have aligned versions of gcc and cmake installed:
MINGW64
# which gcc
/mingw64/bin/gcc
MINGW64
# which cmake
/mingw64/bin/cmake
Now cmake should work properly.
You have these errors because you are trying to compile/link c++ program with a c compiler. For example the two undefined references you are mentioning are part of libstdc++. It is used by default when using g++ but not with CC. If you want to use CC you have to add it manually -lstdc++.
The easiest way is to compile and link c++ programs by using g++.
For some reason the /mingw64/bin/CC.exe is considered as the CXX compiler and the working detection is skipped. to avoid the skipp of the working detection you can add set(CMAKE_CXX_COMPILER_WORKS 1). to modify the compiler it self you can set CMAKE_CXX_COMPILER as explained in CMAKE_CXX_COMPILER or set CXX as explained in CXX . be careful to clean the cache.

Cannot link boost libraries with CMake

I have a sample C++ code that uses boost (program options module) as shown below:
#include <iostream>
#include <boost/program_options/options_description.hpp>
#include <boost/program_options/parsers.hpp>
#include <boost/program_options/variables_map.hpp>
namespace po = boost::program_options;
int main(int argc, const char* argv[]){
po::options_description description("MyTool Usage");
description.add_options()
("help,h", "Display this help message")
("version,v", "Display the version number");
po::positional_options_description p;
po::variables_map vm;
po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm);
po::notify(vm);
if(vm.count("help")){
std::cout << description;
return 0;
}
if(vm.count("version")){
std::cout << "MyTool Version 1.0" << std::endl;
return 0;
}
return 0;
}
I tried to compile using cmake. The corresponding CMakeLists.txt file is shown below:
cmake_minimum_required(VERSION 2.6)
set(CMAKE_CXX_LINK_FLAGS "-std=c++11 -g -Wall -fsigned-char -lboost_program_options-mt")
set(BOOST_ROOT /opt/local/include/boost)
set(BOOST_LIBRARYDIR /opt/local/lib)
find_package(Boost COMPONENTS program_options system filesystem REQUIRED)
include_directories(${BOOST_INCLUDE_DIR})
link_libraries(${BOOST_LIBRARIES})
add_executable(testboostpo testboostpo_simple.cpp)
target_link_libraries(testboostpo ${Boost_LIBRARIES})
Although cmake . seems to succeed and it also detects all the boost modules I typically use (program options, system, filesystem),
$ cmake .
-- The C compiler identification is GNU 5.5.0
-- The CXX compiler identification is GNU 5.5.0
-- Checking whether C compiler has -isysroot
-- Checking whether C compiler has -isysroot - yes
-- Checking whether C compiler supports OSX deployment target flag
-- Checking whether C compiler supports OSX deployment target flag - yes
-- Check for working C compiler: /opt/local/bin/gcc-mp-5
-- Check for working C compiler: /opt/local/bin/gcc-mp-5 -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Checking whether CXX compiler has -isysroot
-- Checking whether CXX compiler has -isysroot - yes
-- Checking whether CXX compiler supports OSX deployment target flag
-- Checking whether CXX compiler supports OSX deployment target flag - yes
-- Check for working CXX compiler: /opt/local/bin/g++-mp-5
-- Check for working CXX compiler: /opt/local/bin/g++-mp-5 -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Boost version: 1.66.0
-- Found the following Boost libraries:
-- program_options
-- system
-- filesystem
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/params/libraries/coding_interview/every_test/boost_test
the next step make fails with the following error:
$ make
Scanning dependencies of target testboostpo
[ 50%] Building CXX object CMakeFiles/testboostpo.dir/testboostpo_simple.cpp.o
[100%] Linking CXX executable testboostpo
ld: library not found for -lboost_program_options-mt
collect2: error: ld returned 1 exit status
make[2]: *** [testboostpo] Error 1
make[1]: *** [CMakeFiles/testboostpo.dir/all] Error 2
make: *** [all] Error 2
There seems to be a problem linking boost library for program options. However, I can see that the library is installed at /opt/local/lib/libboost_program_options-mt.dylib
Alternatively, I also tried isolating the compilation but it fails as well.
$ g++ -L/opt/local/lib testboostpo_simple.cpp -lboost_program_options-mt
Undefined symbols for architecture x86_64:
"boost::program_options::to_internal(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)", referenced from:
std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > boost::program_options::to_internal<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >(std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&) in ccg8tggM.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
$ g++ -L/opt/local/lib testboostpo_simple.cpp /opt/local/lib/libboost_program_options-mt.dylib
Undefined symbols for architecture x86_64:
"boost::program_options::to_internal(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)", referenced from:
std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > boost::program_options::to_internal<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >(std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&) in ccg8tggM.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
I am using gcc5, boost 1.66 and cmake 3.12 (all of them installed using macports).
boost header files are in location:
/opt/local/include/boost/
and the boost library files are in location:
/opt/local/lib
Does anybody here know what might be causing the boost linking to still fail despite providing the paths? Anything else missing here or incorrectly specified?
Thanks,
Because you have used the CMAKE_CXX_LINK_FLAGS, and cmake will check it first instead of using your target_link_libraries, if you want to do it by yourself, you need to add -L/opt/local/lib as well, the simpler way is let cmake do it for you. The following is my example:
cmake_minimum_required(VERSION 2.6)
project(server CXX)
set(CXX_FLAGS
-g
-Wall
)
# set(Boost_NO_SYSTEM_PATHS ON)
set(BOOST_ROOT /mnt/d/Code/boost)
set(BOOST_LIBRARYDIR /mnt/d/Code/boost/stage/lib)
find_package(Threads REQUIRED)
find_package(Boost 1.68.0 COMPONENTS program_options REQUIRED)
file(GLOB SRC
"*cpp*"
)
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
message(${Boost_PROGRAM_OPTIONS_LIBRARIES})
add_executable(main ${SRC})
target_link_libraries(main Threads::Threads ${Boost_PROGRAM_OPTIONS_LIBRARIES})
endif()
Besides, if you build it separately, you should first compile the object then link, or it may turn into undefined reference. In your case, you should link at the last, the result will be different when you put the link in the front or the tail, I am sorry I don't why this happening.
It looks like you're trying to link a 32-bit library against a 64-bit build. The hint is ld: symbol(s) not found for architecture x86_64.
Linking libraries requires that you match the exact build type. Here's some more info about the linking process.
https://www.boost.org/doc/libs/1_63_0/more/getting_started/windows.html

How to build Boost.Beast on linux? The library is on github but won't be included in Boost until December

How to build the following library: Boost.Beast (which will not be available in boost until Boost 1.66.0)?
Reading the build instructions they seem to be focused on Windows only:
cd ..
mkdir bin64
cd bin64
cmake -G"Visual Studio 14 2015 Win64" .. # for 64-bit Windows builds (VS2015)
cmake -G"Visual Studio 15 2017 Win64" .. # for 64-bit Windows builds (VS2017)
I've installed Boost which is a dependency of Beast (include/boost/beast/config.hpp includes <boost/config.hpp>) using:
sudo apt install libboost-dev
I've tried using cmake . and make to build the library which resulted in the below:
user:~/libraries/beast$ cmake .
-- The C compiler identification is GNU 6.3.0
-- The CXX compiler identification is GNU 6.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not found
-- Check if compiler accepts -pthread
-- Check if compiler accepts -pthread - yes
-- Found Threads: TRUE
-- Found OpenSSL: /usr/lib/x86_64-linux-gnu/libssl.so;/usr/lib/x86_64-linux-gnu/libcrypto.so (found version "1.0.2g")
-- Configuring done
-- Generating done
-- Build files have been written to: /home/user/libraries/beast
user:~/libraries/beast$ make
[ 0%] Building CXX object example/advanced/server/CMakeFiles/advanced-server.dir/advanced_server.cpp.o
In file included from /home/user/libraries/beast/include/boost/beast/websocket/stream.hpp:3512:0,
from /home/user/libraries/beast/include/boost/beast/websocket.hpp:18,
from /home/user/libraries/beast/example/advanced/server/advanced_server.cpp:18:
/home/user/libraries/beast/include/boost/beast/websocket/impl/read.ipp: In member function ‘std::size_t boost::beast::websocket::stream<NextLayer>::read_some(const MutableBufferSequence&, boost::beast::error_code&)’:
/home/user/libraries/beast/include/boost/beast/websocket/impl/read.ipp:1077:49: warning: enumeral and non-enumeral type in conditional expression [-Wextra]
cr.code == close_code::none ?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
close_code::normal : cr.code,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[ 0%] Linking CXX executable advanced-server
CMakeFiles/advanced-server.dir/advanced_server.cpp.o: In function `std::thread::thread<main::{lambda()#1}>(main::{lambda()#1}&&)':
advanced_server.cpp:(.text+0x11a9): undefined reference to `pthread_create'
CMakeFiles/advanced-server.dir/advanced_server.cpp.o: In function `__static_initialization_and_destruction_0(int, int)':
advanced_server.cpp:(.text+0x158c): undefined reference to `boost::system::generic_category()'
advanced_server.cpp:(.text+0x1598): undefined reference to `boost::system::generic_category()'
advanced_server.cpp:(.text+0x15a4): undefined reference to `boost::system::system_category()'
CMakeFiles/advanced-server.dir/advanced_server.cpp.o: In function `boost::system::error_code::error_code()':
advanced_server.cpp:(.text._ZN5boost6system10error_codeC2Ev[_ZN5boost6system10error_codeC5Ev]+0x17): undefined reference to `boost::system::system_category()'
CMakeFiles/advanced-server.dir/advanced_server.cpp.o: In function `boost::system::errc::make_error_condition(boost::system::errc::errc_t)':
advanced_server.cpp:(.text._ZN5boost6system4errc20make_error_conditionENS1_6errc_tE[_ZN5boost6system4errc20make_error_conditionENS1_6errc_tE]+0x1c): undefined reference to `boost::system::generic_category()'
CMakeFiles/advanced-server.dir/advanced_server.cpp.o: In function `boost::asio::error::get_system_category()':
advanced_server.cpp:(.text._ZN5boost4asio5error19get_system_categoryEv[_ZN5boost4asio5error19get_system_categoryEv]+0x5): undefined reference to `boost::system::system_category()'
CMakeFiles/advanced-server.dir/advanced_server.cpp.o: In function `boost::beast::file_posix::close(boost::system::error_code&)':
advanced_server.cpp:(.text._ZN5boost5beast10file_posix5closeERNS_6system10error_codeE[_ZN5boost5beast10file_posix5closeERNS_6system10error_codeE]+0x32): undefined reference to `boost::system::generic_category()'
CMakeFiles/advanced-server.dir/advanced_server.cpp.o: In function `boost::beast::file_posix::open(char const*, boost::beast::file_mode, boost::system::error_code&)':
advanced_server.cpp:(.text._ZN5boost5beast10file_posix4openEPKcNS0_9file_modeERNS_6system10error_codeE[_ZN5boost5beast10file_posix4openEPKcNS0_9file_modeERNS_6system10error_codeE]+0x39): undefined reference to `boost::system::generic_category()'
advanced_server.cpp:(.text._ZN5boost5beast10file_posix4openEPKcNS0_9file_modeERNS_6system10error_codeE[_ZN5boost5beast10file_posix4openEPKcNS0_9file_modeERNS_6system10error_codeE]+0x173): undefined reference to `boost::system::generic_category()'
advanced_server.cpp:(.text._ZN5boost5beast10file_posix4openEPKcNS0_9file_modeERNS_6system10error_codeE[_ZN5boost5beast10file_posix4openEPKcNS0_9file_modeERNS_6system10error_codeE]+0x1d8): undefined reference to `boost::system::generic_category()'
CMakeFiles/advanced-server.dir/advanced_server.cpp.o: In function `boost::beast::file_posix::size(boost::system::error_code&) const':
advanced_server.cpp:(.text._ZNK5boost5beast10file_posix4sizeERNS_6system10error_codeE[_ZNK5boost5beast10file_posix4sizeERNS_6system10error_codeE]+0x38): undefined reference to `boost::system::generic_category()'
CMakeFiles/advanced-server.dir/advanced_server.cpp.o:advanced_server.cpp:(.text._ZNK5boost5beast10file_posix4sizeERNS_6system10error_codeE[_ZNK5boost5beast10file_posix4sizeERNS_6system10error_codeE]+0x7e): more undefined references to `boost::system::generic_category()' follow
collect2: error: ld returned 1 exit status
example/advanced/server/CMakeFiles/advanced-server.dir/build.make:94: recipe for target 'example/advanced/server/advanced-server' failed
make[2]: *** [example/advanced/server/advanced-server] Error 1
CMakeFiles/Makefile2:133: recipe for target 'example/advanced/server/CMakeFiles/advanced-server.dir/all' failed
make[1]: *** [example/advanced/server/CMakeFiles/advanced-server.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
The CMakeLists.txt only works for Windows, but the bjam build scripts work on all platforms. The docs could do a better job explaining this. However, note that Beast is header-only so you only need to use bjam or cmake if you are trying to build the examples or tests.

Symbol(s) not found for architecture x86_64 - Cmake - Mac sierra

Recently I have started a new project in C++. The problem is, when I try to compile it I get a linking error. I spent the whole day today trying to debug it, but I did not really find a good solution anywhere. If someone could help with it it would be amazing. I am using a Mac Sierra.
parsing/methylation.h
#ifndef EPIRL_METHYLATION_H
#define EPIRL_METHYLATION_H
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
using namespace std;
namespace methylation {
struct MethLine {
string chr;
int coord;
char strand;
int methylated;
int un_methylated;
string context;
string tag;
};
string calculateMethylationByContext(
MethLine m_input[], int length,
int window_start, int window_end, int threshold);
void calculateMethylation(
const istream &methylation_stream,
const istream &coordinate_stream,
const ostream &output_stream
);
}
#endif //EPIRL_METHYLATION_H
parsing/methylation.cpp
#include "methylation.h"
namespace methylation {
string calculateMethylationByContext(
MethLine m_input[], int length,
int window_start, int window_end, int threshold) {
// rest of the code ...
}
}
main.cpp
#include <iostream>
#include <fstream>
#include "parsing/methylation.h"
using namespace std;
int main(int argc, char **argv) {
if (argc != 4) {
cout << "Invalid number of arguments..." << endl;
return 1;
}
char *methylation_file = argv[1];
char *coordinate_file = argv[2];
char *output_file = argv[3];
ifstream methylation_file_stream(methylation_file, ios::binary);
ifstream coordinate_file_stream(coordinate_file, ios::binary);
ofstream output_file_stream(output_file, ios::binary);
methylation::calculateMethylation(methylation_file_stream,
coordinate_file_stream, output_file_stream);
methylation_file_stream.close();
coordinate_file_stream.close();
output_file_stream.close();
return 0;
}
I use CLion for my coding. When I try to build it, my cmake command works fine, but when I then click 'make' I get the following error:
Undefined symbols for architecture x86_64:
"methylation::calculateMethylation(std::__1::basic_istream<char, std::__1::char_traits<char> > const&, std::__1::basic_istream<char, std::__1::char_traits<char> > const&, std::__1::basic_ostream<char, std::__1::char_traits<char> > const&)", referenced from:
_main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [src] Error 1
make[2]: *** [CMakeFiles/src.dir/all] Error 2
make[1]: *** [CMakeFiles/src.dir/rule] Error 2
make: *** [src] Error 2
my CMakeLists.txt file looks like the following:
cmake_minimum_required(VERSION 3.6)
project(src)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES
parsing/methylation.cpp
parsing/methylation.h
main.cpp)
add_executable(src ${SOURCE_FILES})
When I run the cmake command, my output is this:
-- The C compiler identification is AppleClang 8.0.0.8000042
-- The CXX compiler identification is AppleClang 8.0.0.8000042
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/sztankatt/Documents/University/PartIII/Project/epiRL/src
Your CMakeLists.txt is fine.
As #thomas-matthews #tsyvarev #nos pointed out in their comments, your example code is missing the definition/implementation of methylation::calculateMethylation(). What you're seeing is the expected failure with Apple/clang in this situation.
❯ make
[ 33%] Building CXX object CMakeFiles/src.dir/parsing/methylation.cpp.o
/Users/nega/foo/parsing/methylation.cpp:8:5: warning: control reaches end of non-void function [-Wreturn-type]
}
^
1 warning generated.
[ 66%] Building CXX object CMakeFiles/src.dir/main.cpp.o
[100%] Linking CXX executable src
Undefined symbols for architecture x86_64:
"methylation::calculateMethylation(std::__1::basic_istream<char, std::__1::char_traits<char> > const&, std::__1::basic_istream<char, std::__1::char_traits<char> > const&, std::__1::basic_ostream<char, std::__1::char_traits<char> > const&)", referenced from:
_main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [src] Error 1
make[1]: *** [CMakeFiles/src.dir/all] Error 2
make: *** [all] Error 2
Adding a dummy implementation, or commenting out the call in your main.cpp, will allow make to complete successfully.
Assuming you have a correct implementation
Let's assume you do have an implementation of methylation::calculateMethylation() in your code (maybe in another file). The fist step in debugging build errors in CMake generated Makefiles is to run with the make variable VERBOSE set to a true value, ie: make VERBOSE=1.
❯ make VERBOSE=1
/usr/local/Cellar/cmake/3.7.0/bin/cmake -H/Users/nega/foo -B/Users/nega/foo/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/local/Cellar/cmake/3.7.0/bin/cmake -E cmake_progress_start /Users/nega/foo/build/CMakeFiles /Users/nega/foo/build/CMakeFiles/progress.marks
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/Makefile2 all
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/src.dir/build.make CMakeFiles/src.dir/depend
cd /Users/nega/foo/build && /usr/local/Cellar/cmake/3.7.0/bin/cmake -E cmake_depends "Unix Makefiles" /Users/nega/foo /Users/nega/foo /Users/nega/foo/build /Users/nega/foo/build /Users/nega/foo/build/CMakeFiles/src.dir/DependInfo.cmake --color=
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/src.dir/build.make CMakeFiles/src.dir/build
[ 33%] Building CXX object CMakeFiles/src.dir/parsing/methylation.cpp.o
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -std=gnu++11 -o CMakeFiles/src.dir/parsing/methylation.cpp.o -c /Users/nega/foo/parsing/methylation.cpp
/Users/nega/foo/parsing/methylation.cpp:8:5: warning: control reaches end of non-void function [-Wreturn-type]
}
^
1 warning generated.
[ 66%] Building CXX object CMakeFiles/src.dir/main.cpp.o
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -std=gnu++11 -o CMakeFiles/src.dir/main.cpp.o -c /Users/nega/foo/main.cpp
[100%] Linking CXX executable src
/usr/local/Cellar/cmake/3.7.0/bin/cmake -E cmake_link_script CMakeFiles/src.dir/link.txt --verbose=1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/src.dir/parsing/methylation.cpp.o CMakeFiles/src.dir/main.cpp.o -o src
Undefined symbols for architecture x86_64:
"methylation::calculateMethylation(std::__1::basic_istream<char, std::__1::char_traits<char> > const&, std::__1::basic_istream<char, std::__1::char_traits<char> > const&, std::__1::basic_ostream<char, std::__1::char_traits<char> > const&)", referenced from:
_main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [src] Error 1
make[1]: *** [CMakeFiles/src.dir/all] Error 2
make: *** [all] Error 2
Now you can look at the link step and see if you're missing items; maybe a library, or an object file. If so, now you know to go back and add it to your CMakeLists.txt
Summary
The first step in debugging unexpected build failures with CMake generated Makefiles is to run:
❯ make VERBOSE=1
This will give you insight into what CMake is doing behind the scenes.
Double check which compiler is automatically detected by CMAKE.
Can you post, what CMAKE tells you when you initially run CMAKE?
This may be a hint for your problem, too

OpenCV 3.0 source code installation troubleshooting

I installed OpenCV3.0 using the source code obtained from itseez github repo. I set the install prefix as /home/ubuntu/installed_libs/. All went well. I even set ldconfig path in /etc/ld.so.conf.d/local.conf and then sudo ldconfig.
However in my project (CMake) I cannot find the newly installed directory.
When I do cmake ../ in the build directory it throws the following error.
I did some changes as suggested in the answers.
cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
set(OpenCV_DIR /home/ubuntu/installed_libs/share/OpenCV)
find_package( OpenCV )
add_executable( DisplayImage DisplayImage.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBRARIES} )
message(STATUS "OpenCV_FOUND=${OpenCV_FOUND}")
message(STATUS "OpenCV_INCLUDES=${OpenCV_INCLUDE_DIR}")
message(STATUS "OpenCV_LIBRARIES=${OpenCV_LIBRARIES}")
message(STATUS "OpenCV_DEFINATIONS=${OpenCV_DEFINATIONS}")
message(STATUS "OpenCV_DIR=${OpenCV_DIR}")
Now cmake happens successfully.
$ cmake ..
-- The C compiler identification is GNU 4.8.4
-- The CXX compiler identification is GNU 4.8.4
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Found CUDA: /usr/local/cuda (found suitable exact version "6.5")
-- OpenCV_FOUND=1
-- OpenCV_INCLUDES=
-- OpenCV_LIBRARIES=opencv_videostab;opencv_videoio;opencv_video;opencv_ts;opencv_superres;opencv_stitching;opencv_shape;opencv_photo;opencv_objdetect;opencv_ml;opencv_imgproc;opencv_imgcodecs;opencv_highgui;opencv_hal;opencv_flann;opencv_features2d;opencv_cudev;opencv_cudawarping;opencv_cudastereo;opencv_cudaoptflow;opencv_cudaobjdetect;opencv_cudalegacy;opencv_cudaimgproc;opencv_cudafilters;opencv_cudafeatures2d;opencv_cudacodec;opencv_cudabgsegm;opencv_cudaarithm;opencv_core;opencv_calib3d
-- OpenCV_DEFINATIONS=
-- OpenCV_DIR=/home/ubuntu/installed_libs/share/OpenCV
-- Configuring done
-- Generating done
-- Build files have been written to: /home/ubuntu/ctry/opencvtest/build
However, when I do a make, I get an error :
Scanning dependencies of target DisplayImage
[100%] Building CXX object CMakeFiles/DisplayImage.dir/DisplayImage.cpp.o
Linking CXX executable DisplayImage
CMakeFiles/DisplayImage.dir/DisplayImage.cpp.o: In function `main':
DisplayImage.cpp:(.text+0x94): undefined reference to `cv::imread(std::string const&, int)'
DisplayImage.cpp:(.text+0x108): undefined reference to `cv::namedWindow(std::string const&, int)'
DisplayImage.cpp:(.text+0x14e): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
DisplayImage.cpp:(.text+0x15e): undefined reference to `cv::imshow(std::string const&, cv::_InputArray const&)'
collect2: error: ld returned 1 exit status
make[2]: *** [DisplayImage] Error 1
make[1]: *** [CMakeFiles/DisplayImage.dir/all] Error 2
make: *** [all] Error 2
I know this is a linking error. How should I fix this?
Verbose make display incase it is helpful.
$ make VERBOSE=1
/usr/bin/cmake -H/home/ubuntu/ctry/opencvtest -B/home/ubuntu/ctry/opencvtest/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /home/ubuntu/ctry/opencvtest/build/CMakeFiles /home/ubuntu/ctry/opencvtest/build/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory `/home/ubuntu/ctry/opencvtest/build'
make -f CMakeFiles/DisplayImage.dir/build.make CMakeFiles/DisplayImage.dir/depend
make[2]: Entering directory `/home/ubuntu/ctry/opencvtest/build'
cd /home/ubuntu/ctry/opencvtest/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/ubuntu/ctry/opencvtest /home/ubuntu/ctry/opencvtest /home/ubuntu/ctry/opencvtest/build /home/ubuntu/ctry/opencvtest/build /home/ubuntu/ctry/opencvtest/build/CMakeFiles/DisplayImage.dir/DependInfo.cmake --color=
make[2]: Leaving directory `/home/ubuntu/ctry/opencvtest/build'
make -f CMakeFiles/DisplayImage.dir/build.make CMakeFiles/DisplayImage.dir/build
make[2]: Entering directory `/home/ubuntu/ctry/opencvtest/build'
Linking CXX executable DisplayImage
/usr/bin/cmake -E cmake_link_script CMakeFiles/DisplayImage.dir/link.txt --verbose=1
/usr/bin/c++ CMakeFiles/DisplayImage.dir/DisplayImage.cpp.o -o DisplayImage -L/usr/local/cuda/lib -rdynamic /home/ubuntu/installed_libs/lib/libopencv_videostab.so.3.0.0 /home/ubuntu/installed_libs/lib/libopencv_videoio.so.3.0.0 /home/ubuntu/installed_libs/lib/libopencv_video.so.3.0.0 /home/ubuntu/installed_libs/lib/libopencv_ts.a /home/ubuntu/installed_libs/lib/libopencv_superres.so.3.0.0 /home/ubuntu/installed_libs/lib/libopencv_stitching.so.3.0.0 /home/ubuntu/installed_libs/lib/libopencv_shape.so.3.0.0 /home/ubuntu/installed_libs/lib/libopencv_photo.so.3.0.0 /home/ubuntu/installed_libs/lib/libopencv_objdetect.so.3.0.0 /home/ubuntu/installed_libs/lib/libopencv_ml.so.3.0.0 /home/ubuntu/installed_libs/lib/libopencv_imgproc.so.3.0.0 /home/ubuntu/installed_libs/lib/libopencv_imgcodecs.so.3.0.0 /home/ubuntu/installed_libs/lib/libopencv_highgui.so.3.0.0 /home/ubuntu/installed_libs/lib/libopencv_hal.a /home/ubuntu/installed_libs/lib/libopencv_flann.so.3.0.0 /home/ubuntu/installed_libs/lib/libopencv_features2d.so.3.0.0 /home/ubuntu/installed_libs/lib/libopencv_cudev.so.3.0.0 /home/ubuntu/installed_libs/lib/libopencv_cudawarping.so.3.0.0 /home/ubuntu/installed_libs/lib/libopencv_cudastereo.so.3.0.0 /home/ubuntu/installed_libs/lib/libopencv_cudaoptflow.so.3.0.0 /home/ubuntu/installed_libs/lib/libopencv_cudaobjdetect.so.3.0.0 /home/ubuntu/installed_libs/lib/libopencv_cudalegacy.so.3.0.0 /home/ubuntu/installed_libs/lib/libopencv_cudaimgproc.so.3.0.0 /home/ubuntu/installed_libs/lib/libopencv_cudafilters.so.3.0.0 /home/ubuntu/installed_libs/lib/libopencv_cudafeatures2d.so.3.0.0 /home/ubuntu/installed_libs/lib/libopencv_cudacodec.so.3.0.0 /home/ubuntu/installed_libs/lib/libopencv_cudabgsegm.so.3.0.0 /home/ubuntu/installed_libs/lib/libopencv_cudaarithm.so.3.0.0 /home/ubuntu/installed_libs/lib/libopencv_core.so.3.0.0 /home/ubuntu/installed_libs/lib/libopencv_calib3d.so.3.0.0 /home/ubuntu/installed_libs/lib/libopencv_cudawarping.so.3.0.0 /home/ubuntu/installed_libs/lib/libopencv_objdetect.so.3.0.0 /home/ubuntu/installed_libs/lib/libopencv_cudafilters.so.3.0.0 /home/ubuntu/installed_libs/lib/libopencv_cudaarithm.so.3.0.0 /home/ubuntu/installed_libs/lib/libopencv_features2d.so.3.0.0 /home/ubuntu/installed_libs/lib/libopencv_ml.so.3.0.0 /home/ubuntu/installed_libs/lib/libopencv_highgui.so.3.0.0 /home/ubuntu/installed_libs/lib/libopencv_videoio.so.3.0.0 /home/ubuntu/installed_libs/lib/libopencv_imgcodecs.so.3.0.0 /home/ubuntu/installed_libs/lib/libopencv_flann.so.3.0.0 /home/ubuntu/installed_libs/lib/libopencv_video.so.3.0.0 /home/ubuntu/installed_libs/lib/libopencv_imgproc.so.3.0.0 /home/ubuntu/installed_libs/lib/libopencv_core.so.3.0.0 /home/ubuntu/installed_libs/lib/libopencv_cudev.so.3.0.0 /home/ubuntu/installed_libs/lib/libopencv_hal.a -ldl -lm -lpthread -lrt -lcudart -lnppc -lnppi -lnpps -lcufft -lcudart -lnppc -lnppi -lnpps -lcufft -Wl,-rpath,/usr/local/cuda/lib:/home/ubuntu/installed_libs/lib
CMakeFiles/DisplayImage.dir/DisplayImage.cpp.o: In function `main':
DisplayImage.cpp:(.text+0x94): undefined reference to `cv::imread(std::string const&, int)'
DisplayImage.cpp:(.text+0x108): undefined reference to `cv::namedWindow(std::string const&, int)'
DisplayImage.cpp:(.text+0x14e): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
DisplayImage.cpp:(.text+0x15e): undefined reference to `cv::imshow(std::string const&, cv::_InputArray const&)'
collect2: error: ld returned 1 exit status
make[2]: *** [DisplayImage] Error 1
make[2]: Leaving directory `/home/ubuntu/ctry/opencvtest/build'
make[1]: *** [CMakeFiles/DisplayImage.dir/all] Error 2
make[1]: Leaving directory `/home/ubuntu/ctry/opencvtest/build'
make: *** [all] Error 2
You should check the output of:
ldconfig -p | grep -i opencv
pkg-config --cflags opencv
pkg-config --libs opencv
If nothing is listed, then you ldconfig may configured incorrectly. If the system-installed version of OpenCV is listed, it may be a problem of the search order.
In either case, you could try to configure the CMAKE_MODULE_PATH of your project DisplayImage, which is searched by CMake before standard system locations. So, assuming your in your build directory of DisplayImage is $SRC_DIR/build, configure your project like this
cmake -DCMAKE_MODULE_PATH="/home/ubuntu/installed_libs/<OpenCV directory>" <other options> ..
Also interesting: what happens in the background of find_package().
You can add
set(OpenCV_DIR /home/ubuntu/installed_libs/share/OpenCV)
to your CMakeLists, so it looks something like
cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
set(OpenCV_DIR /home/ubuntu/installed_libs/share/OpenCV)
find_package( OpenCV REQUIRED 3.0 )
add_executable( DisplayImage DisplayImage.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )