0

I'm trying to set up the CPLEX C++ API without success. I'm using a CMakeLists.txt and the CLion IDE.

The cplex and concert API files are included, and the libraries are added to the linker. The CMakeLists.txt looks as follows.

cmake_minimum_required(VERSION 3.23)
project(cpp_cplex)

include_directories("/opt/ibm/ILOG/CPLEX_Studio201/cplex/include"
        "/opt/ibm/ILOG/CPLEX_Studio201/concert/include")

link_directories("/opt/ibm/ILOG/CPLEX_Studio201/cplex/lib/x86-64_linux/static_pic"
        "/opt/ibm/ILOG/CPLEX_Studio201/concert/lib/x86-64_linux/static_pic")

set(CMAKE_CXX_STANDARD 14)

add_executable(cpp_cplex main.cpp)

There are no errors in the source code. Yet execution fails with the error that IloModel cannot be resolved.

1/1] Linking CXX executable cpp_cplex
FAILED: cpp_cplex 
: && /usr/bin/c++ -g  CMakeFiles/cpp_cplex.dir/main.cpp.o -o cpp_cplex -L/opt/ibm/ILOG/CPLEX_Studio201/cplex/lib/x86-64_linux/static_pic   -L/opt/ibm/ILOG/CPLEX_Studio201/concert/lib/x86-64_linux/static_pic  && :
/usr/bin/ld: CMakeFiles/cpp_cplex.dir/main.cpp.o: in function `main':
/home/x/workspace/cpp-cplex/main.cpp:14: undefined reference to `IloModel::IloModel(IloEnv, char const*)'
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.

I'm doing this for the first time, so I don't really know where to go from here. Though, I was able to compile manually with:

g++ -m64 -fPIC -fno-strict-aliasing -fexceptions -DNDEBUG -I/opt/ibm/ILOG/CPLEX_Studio201/cplex/include -I/opt/ibm/ILOG/CPLEX_Studio201/concert/include -L/opt/ibm/ILOG/CPLEX_Studio201/cplex/lib/x86-64_linux/static_pic -L/opt/ibm/ILOG/CPLEX_Studio201/concert/lib/x86-64_linux/static_pic  -o main main.cpp -lconcert -lilocplex -lcplex -lm -lpthread -ldl

Now, it should somehow be possible to transfer this to cmake.

Christian
  • 1,308
  • 3
  • 14
  • 24
  • 1
    "... and the libraries are added to the linker" - The command `link_directories` specifies directories for **search** libraries. You still need to specify libraries for link with `target_link_libraries`. – Tsyvarev Oct 10 '22 at 08:02
  • @Tsyvarev: I knew this was an easy question for someone who knows this stuff - you made my day! I added all the libraries from the folders and now it works. To that end, I created a variable for each library like so `set(CPLEX_ILO_LIBRARY "/opt/ibm/ILOG/CPLEX_Studio201/cplex/lib/x86-64_linux/static_pic/libilocplex.a")`. I then concatenated all libraries like so `set(CPLEX_LIBRARIES "${CPLEX_ILO_LIBRARY};${CPLEX_CONCERT_LIBRARY};${CPLEX_LIBRARY};${CPLEX_MIP_LIBRARY};" )`, and finally added them with `target_link_libraries(cpp_cplex PUBLIC ${CPLEX_LIBRARIES})`. – Christian Oct 10 '22 at 13:38

0 Answers0