当代码使用 C++ 但调用 python 时如何编写 CMakeFile
How to write CMakeFile when the code use C++, but called python
我有一个带有 CMakeFile 的 C++ 项目,之前工作正常。
现在我用 C++ 写了一个新的 class,但是调用 python 到 运行。我知道如何编写一个 MakeFile 来使用 python 构建单个 C++,但让我困惑的是如何根据我的项目编写 CMakeFile。
我的 MakeFile 来了 class:
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++写的puclass调用python。我知道CMakeFile会编译成MakeFile,然后编译源代码。
如何将新 Class 的 MakeFile 集成到我的 odl CMakeFile
这应该足够了:
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
我有一个带有 CMakeFile 的 C++ 项目,之前工作正常。
现在我用 C++ 写了一个新的 class,但是调用 python 到 运行。我知道如何编写一个 MakeFile 来使用 python 构建单个 C++,但让我困惑的是如何根据我的项目编写 CMakeFile。
我的 MakeFile 来了 class:
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++写的puclass调用python。我知道CMakeFile会编译成MakeFile,然后编译源代码。
如何将新 Class 的 MakeFile 集成到我的 odl CMakeFile
这应该足够了:
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