当代码使用C ++但是调用python时如何编写CMakeFile

时间:2015-05-23 19:17:31

标签: python c++ linux makefile cmake

我有一个带有CMakeFile的C ++项目,以前工作正常。

现在我用C ++编写一个新类,但是调用python来运行。我知道如何编写一个MakeFile来构建带有python的单个C ++,但令我困惑的是如何根据我的项目编写CMakeFile。

我的MakeFile包含单个类:

outxx:
    g++ -I/usr/include/python2.7 -L/usr/lib/ pu.h pu.cpp main.cpp -lpython2.7 -o outxx -g
clean:
    rm -rf *.o *.pyc  outxx

由C ++编写但是调用python的pu类。我知道CMakeFile将编译为MakeFile,然后编译源代码。

如何将新Class的MakeFile插入我的odl CM​​akeFile

1 个答案:

答案 0 :(得分:3)

这应该足够了:

find_package(PythonLibs REQUIRED)

add_executable(outxx main.cpp pu.cpp)
target_link_libraries(outxx ${PYTHON_LIBRARIES})
target_include_directories(outxx PUBLIC ${PYTHON_INCLUDE_DIRS})

如果我们尝试运行它:

[2:28pm][wlynch@watermelon blah] cmake .
-- The C compiler identification is AppleClang 6.1.0.6020053
-- The CXX compiler identification is AppleClang 6.1.0.6020053
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/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: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found PythonLibs: /usr/lib/libpython2.7.dylib (found version "2.7.6") 
CMake Warning (dev) in CMakeLists.txt:
  No cmake_minimum_required command is present.  A line of code such as

    cmake_minimum_required(VERSION 3.2)

  should be added at the top of the file.  The version specified may be lower
  if you wish to support older CMake versions for this project.  For more
  information run "cmake --help-policy CMP0000".
This warning is for project developers.  Use -Wno-dev to suppress it.

-- Configuring done
-- Generating done
-- Build files have been written to: /tmp/blah


[2:29pm][wlynch@watermelon blah] make VERBOSE=1
/Users/wlynch/Homebrew/Cellar/cmake/3.2.2/bin/cmake -H/tmp/blah -B/tmp/blah --check-build-system CMakeFiles/Makefile.cmake 0
/Users/wlynch/Homebrew/Cellar/cmake/3.2.2/bin/cmake -E cmake_progress_start /tmp/blah/CMakeFiles /tmp/blah/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/outxx.dir/build.make CMakeFiles/outxx.dir/depend
cd /tmp/blah && /Users/wlynch/Homebrew/Cellar/cmake/3.2.2/bin/cmake -E cmake_depends "Unix Makefiles" /tmp/blah /tmp/blah /tmp/blah /tmp/blah /tmp/blah/CMakeFiles/outxx.dir/DependInfo.cmake --color=
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/outxx.dir/build.make CMakeFiles/outxx.dir/build
/Users/wlynch/Homebrew/Cellar/cmake/3.2.2/bin/cmake -E cmake_progress_report /tmp/blah/CMakeFiles 1
[ 50%] Building CXX object CMakeFiles/outxx.dir/main.o
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++    -I/System/Library/Frameworks/Python.framework/Headers    -o CMakeFiles/outxx.dir/main.o -c /tmp/blah/main.cpp
/Users/wlynch/Homebrew/Cellar/cmake/3.2.2/bin/cmake -E cmake_progress_report /tmp/blah/CMakeFiles 2
[100%] Building CXX object CMakeFiles/outxx.dir/pu.o
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++    -I/System/Library/Frameworks/Python.framework/Headers    -o CMakeFiles/outxx.dir/pu.o -c /tmp/blah/pu.cpp
Linking CXX executable outxx
/Users/wlynch/Homebrew/Cellar/cmake/3.2.2/bin/cmake -E cmake_link_script CMakeFiles/outxx.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/outxx.dir/main.o CMakeFiles/outxx.dir/pu.o  -o outxx  /usr/lib/libpython2.7.dylib 
/Users/wlynch/Homebrew/Cellar/cmake/3.2.2/bin/cmake -E cmake_progress_report /tmp/blah/CMakeFiles  1 2
[100%] Built target outxx
/Users/wlynch/Homebrew/Cellar/cmake/3.2.2/bin/cmake -E cmake_progress_start /tmp/blah/CMakeFiles 0