【问题标题】:How to write CMakeFile when the code use C++, but called python代码使用C++时如何编写CMakeFile,但调用python
【发布时间】:2015-05-23 19:17:31
【问题描述】:

我有一个带有 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++写的pu类调用python。我知道 CMakeFile 会编译成 MakeFile 然后编译源代码。

如何将新类的 MakeFile 集成到我的 odl CM​​akeFile 中

【问题讨论】:

    标签: python c++ linux makefile cmake


    【解决方案1】:

    这就足够了:

    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
    

    【讨论】:

    • 谢谢比尔,它对我有帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-19
    • 2012-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-09
    相关资源
    最近更新 更多