简体   繁体   中英

Converting a Makefile into CMakeLists.txt equivalent

I am working on a project were I maintain a CMakeLists.txt file to keep track of the dependencies when I need to compile.

I recently, started using a new library that I need to integrate in my project. The library comes with some examples on how to compile but this is purely a Makefile. I would like to integrate the logic of the Makefile into my project's CMakeLists.txt file.

The Makefile of this library is as follows:

COMMON=-O2 -I/home/john/.mujoco/mjpro200/include -L/home/john/.mujoco/mjpro200/bin -std=c++11 -mavx -pthread -Wl,-rpath,'$$ORIGIN'

all:
    g++ $(COMMON) basic.cpp      -lmujoco200 -lGL -lglew /home/john/.mujoco/mjpro200/bin/libglfw.so.3 -o basic
    gcc -c -O2 -mavx -I/home/john/.mujoco/mjpro200/include /home/john/.mujoco/mjpro200/include/uitools.c
    rm *.o

I would like to convert this Makefile into CMakeLists.txt exactly if possible. I see that they provide some flags for threading ( pthread ) and I want to keep these flags and settings in my CMakeLists.txt in case they are needed for performance.

Here is my CMakeLists.txt:

cmake_minimum_required (VERSION 2.6.0)
project(myproject)
add_compile_options(-std=c++11) # CMake 2.8.12 or newer

if( CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX )
  add_definitions("-fno-strict-aliasing -Wall")
endif( CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX )

# optional in case boost is used
find_package(Boost 1.58 EXACT)
find_package(Eigen REQUIRED)

set(mujocoCommon "-O2 -I/home/john/.mujoco/mjpro200/include -L/home/john/.mujoco/mjpro200/bin -std=c++11 -mavx -pthread -Wl,-rpath,'$$ORIGIN'")

set(Mujoco_INCLUDE_DIRS "/home/john/.mujoco/mjpro200/include")

include_directories(${Boost_INCLUDE_DIRS} ${Eigen_INCLUDE_DIRS} ${Mujoco_INCLUDE_DIRS})
link_directories(${Boost_LIBRARY_DIRS})
add_executable(myproject mycode.cpp)
install(TARGETS myproject DESTINATION .)

So I tried to define a variable mujocoCommon similar to the COMMON in the Makefile as well as the Mujoco_INCLUDE_DIRS that I then use in the CMake's include_directories .

Note that in the myproject.cpp I have MuJoCo code and other dependencies that I need to correctly link against. In the Makefile things looks a bit more simpler because in there the code has only MuJoco dependencies.

I need some heads-up on how to add in my CMakeLists.txt the following:

  • -mavx -pthread -Wl,-rpath,'$$ORIGIN' part of Makefile's COMMON variable.
  • -lmujoco200 -lGL -lglew /home/john/.mujoco/mjpro200/bin/libglfw.so.3
  • -c -O2 -mavx -I/home/john/.mujoco/mjpro200/include /home/john/.mujoco/mjpro200/include/uitools.c

Currently with the minimal knowledge on CMake and what I have in my CMakeLists.txt when I compile using "make" I get many errors of the kind:

myproject.cpp:(.text+0xfa9): undefined reference to `glfwSetMouseButtonCallback'

Basically I would like to link (?) my code with MuJoco library as is done in their Makefile.

Thanks.

Edit In regards with the possible duplicate flag, I added the following flag:

set(Mujoco_LIBRARIES "/home/john/.mujoco/mjpro200/bin")
target_link_libraries(myproject ${Mujoco_LIBRARIES} -lboost_system)

But it didn't solved the problem.

Edit 2:

I have also added this one:

set(CMAKE_CXX_FLAGS "-lmujoco200 -lGL -lglew /home/john/.mujoco/mjpro200/bin/libglfw.so.3")

The errors I am getting:

/usr/bin/x86_64-linux-gnu-ld: cannot find -lmujoco200
/usr/bin/x86_64-linux-gnu-ld: cannot find -lglew

You also need to add the opengl libraries to the link libraries, target_link_libraries . Don't add them with -l .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM