CMake generated Makefile throws link error
Hello,
My question, in general, is about CMake-generate Makefile that throws a linking error, even though the CMake successfully finds all of the libraries and the "include" directory. The make command on my own computer generates the executable file with no error, however, It fails on Amazon AWS ubuntu instances. I am not sure what is different on an AWS machine for instance.
Here, I am trying to write a CMake file for one simple Gurobi example like https://www.gurobi.com/documentation/8.1/examples/lp_cpp_cpp.html. Here, CMake successfully processes my CMakeLists.txt file, however, the resulting Makefile throws an error. I have the following CMake file
project(lp_cpp)
cmake_minimum_required(VERSION 2.8)
aux_source_directory(. SRC_LIST)
add_executable(${PROJECT_NAME} ${SRC_LIST})
#SET (CMAKE_CXX_COMPILER "g++")
list( APPEND CMAKE_CXX_FLAGS "-std=c++0x ${CMAKE_CXX_FLAGS} -g -ftest-coverage -fprofile-arcs")
find_library( GUROBI_LIBRARY
NAMES gurobi81
PATHS "$ENV{GUROBI_HOME}/lib/"
)
find_library( GUROBI_CXX_LIBRARY
NAMES gurobi_c++
PATHS "$ENV{GUROBI_HOME}/lib/"
)
if(NOT GUROBI_CXX_LIBRARY)
message(FATAL_ERROR "GUROBI_CXX_LIBRARY not found!")
else()
message("GUROBI_CXX_LIBRARY found at:" ${GUROBI_CXX_LIBRARY})
endif()
if(NOT GUROBI_LIBRARY)
message(FATAL_ERROR "GUROBI_LIBRARY not found!")
else()
message("GUROBI_LIBRARY found at:" ${GUROBI_LIBRARY})
endif()
find_path(GUROBI_INCLUDE_DIR
NAMES gurobi_c++.h
PATHS "$ENV{GUROBI_HOME}/include/"
)
if(NOT GUROBI_INCLUDE_DIR)
message(FATAL_ERROR "GUROBI_INCLUDE_DIR not found!")
else()
message("GUROBI_INCLUDE_DIR found at:" ${GUROBI_INCLUDE_DIR})
endif()
include_directories(${GUROBI_INCLUDE_DIR})
LINK_DIRECTORIES("$ENV{GUROBI_HOME}/lib/")
target_link_libraries(lp_cpp ${GUROBI_CXX_LIBRARY} ${GUROBI_LIBRARY})
After running cmake, I get the following output:
GUROBI_CXX_LIBRARY found at:/opt/gurobi811/linux64/lib/libgurobi_c++.a
GUROBI_LIBRARY found at:/opt/gurobi811/linux64/lib/libgurobi81.so
GUROBI_INCLUDE_DIR found at:/opt/gurobi811/linux64/include
-- Configuring done
-- Generating done
-- Build files have been written to: /home/ubuntu/lp_c/build
However, running make will throw the linking error:
CMakeFiles/lp_cpp.dir/lp_c++.cpp.o: In function `main':
/home/ubuntu/lp_c/lp_c++.cpp:23: undefined reference to `GRBModel::GRBModel(GRBEnv const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/home/ubuntu/lp_c/lp_c++.cpp:44: undefined reference to `GRBModel::write(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/home/ubuntu/lp_c/lp_c++.cpp:54: undefined reference to `GRBException::getMessage[abi:cxx11]() const'
collect2: error: ld returned 1 exit status
CMakeFiles/lp_cpp.dir/build.make:96: recipe for target 'lp_cpp' failed
make[2]: *** [lp_cpp] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/lp_cpp.dir/all' failed
make[1]: *** [CMakeFiles/lp_cpp.dir/all] Error 2
Please sign in to leave a comment.
Comments
0 comments