problem : Recently, when writing a project, I need to use python3, But because of the introduction of ROS Relevant environment , Lead to the desire to use python3 That part of the code has been used by default ROS Medium python2, So the environment is wrong .
solution : The logical idea is for the need python3 The part of the code that specifies python3 Environment , This needs to be in cmakelist Separately indicated in
You may often see several open source projects CmakeLists.txt, The purpose of this is essentially to break the code into many pieces , Each piece has a separate CmakeLists.txt management , Finally, just a general CmakeLists.txt Just summarize . Be careful : This is where a piece of code CmakeLists.txt We can complete the functions we want . The simplest example is as follows , We have a hello library + One world library , The two of them put together a final code base , There is the following project directory structure :
And we want to control one of the code blocks python Version and call this part of the code in the main file , I Suggested code format by :
project_name/
——cmake
——sub_project_name.cmake
—— include
——src
——CmakeLists.txt( Lord )
——third_party
——sub_project_name
——include
——src
——CmakeLists.txt( Son )
In the above structure ,project_name Home folder ,sub_project_name Because we want to control python In the subfolder of the code block of the version code , We put him in third_party Easy to manage . The next step is to write CmakeLists.txt 了 , See me for how to write it Previous post .
In the sub library CmakeList.txt The writing method is basically the same as that under normal circumstances , Just will add_executable Replace for add_library, We need to specify exactly python edition
cmake_minimum_required(VERSION 3.12.0)
project(CallAPI)
set( CMAKE_CXX_FLAGS "-std=c++14 -O3" )
SET(Python3_INCLUDE_DIRS "/usr/include/python3.6m")
SET(Python3_LIBRARIES "/usr/lib/python3.6/config-3.6m-x86_64-linux-gnu/libpython3.6.so")
SET( PYTHON_EXECUTABLE /usr/bin/python3.6)
# find_package(Python3 COMPONENTS Interpreter Development REQUIRED)
message("Python3: " ${
Python3_INCLUDE_DIRS})
message("Python3: " ${
Python3_LIBRARIES})
include_directories(./include/ ${
Python3_INCLUDE_DIRS})
add_library(libCallAPI src/call_api.cpp)
target_link_libraries(libCallAPI ${
Python3_LIBRARIES})
As I said in my previous article , Here, I suggest you write a sub_project_name.cmake File to connect to the library , In my writing CallAPI Library as an example ,.cmake The contents are as follows
add_subdirectory(${
PROJECT_SOURCE_DIR}/thirdparty/CallAPI)
include_directories(${
PROJECT_SOURCE_DIR}/thirdparty/CallAPI/include/)
list(APPEND ALL_TARGET_LIBRARIES libCallAPI)
With .cmake file , In the main CmakeLists.txt You only need to apply this file in
... Other content ...
include(cmake/callapi.cmake)
... Other content ...
Above ,sub_project_name Library python Version is your specific version , Will not receive the Lord or other CmakeLists.txt The impact of . The lines , The above method can also be used when other dependent multi version environments are required .
Committed to quantitative stra
use Python Show the distributi