python生成c再编译_Cmake编译python到构建文件夹

I think an example would be the best way to demonstrate my problem:

Lets say I have a source directory named src. This directory has two files, a CMakeLists.txt file and a python file named blah. In the same place where src is stored, there is a folder named build. Now, I would like to be able to run cmake and make it compile my python file blah and place it into my build directory. Currently, my CMakeLists.txt is just copying the uncompiled code straight into my build directory using the call:

CONFIGURE_FILE(${PATH_TO_SOURCE}/blah.py ${PATH_TO_BUILD}/blah.py COPYONLY)

Then I run make in the terminal to compile the actual file. This leaves the uncompiled python file in my build directory, which is no good. I've tried using ADD_EXECUTABLES, creating a custom target and using COMMAND, but my syntax must be off somewhere. Any help would be greatly appreciated!

解决方案

CMake doesn't support Python out of the box, AFAIK. because of this, you will need to write rule to build .py files, something like that:

macro(add_python_target tgt)

foreach(file ${ARGN})

set(OUT ${CMAKE_CURRENT_BINARY_DIR}/${file}.pyo)

list(APPEND OUT_FILES ${OUT})

add_custom_command(OUTPUT ${OUT}

COMMAND )

endforeach()

add_custom_target(${tgt} ALL

DEPENDS ${OUT_FILES}

endmacro()

After defining it, you can use it this way:

add_python_target(blabla blah.py)

If you want to make adjustmenets or need more information, see CMake documentation.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Windows下使用CMake编译g2o库并生成Python库,需要进行以下步骤: 1. 下载安装CMake 首先需要下载并安装CMake,官网下载链接为:https://cmake.org/download/,选择Windows版本的安装包进行下载和安装。 2. 下载安装Eigen库 g2o库依赖Eigen库,需要先下载并安装Eigen库。Eigen库的官网为:http://eigen.tuxfamily.org/index.php?title=Main_Page,可以在官网下载对应版本的Eigen库。 下载完成后,将Eigen库解压缩到某个目录下,并记住该目录的路径。 3. 下载安装MinGW 在Windows下编译g2o库,需要使用MinGW作为编译工具链。可以通过MinGW官网下载并安装MinGW:https://sourceforge.net/projects/mingw/。 安装完成后,需要将MinGW的bin目录添加到系统环境变量中,以便在命令行中使用MinGW。 4. 下载安装Python和Boost.Python库 如果需要将g2o编译Python库,还需要安装Python和Boost.Python库。 Python的官网为:https://www.python.org/downloads/,选择对应版本的Python进行下载和安装。 Boost.Python库的官网为:https://www.boost.org/doc/libs/1_76_0/more/getting_started/windows.html,可以在官网下载对应版本的Boost库。 下载完成后,将Boost库解压缩到某个目录下,并记住该目录的路径。 5. 下载并编译g2o库 在官方的github库中下载g2o的源代码,链接为:https://github.com/RainerKuemmerle/g2o。 下载完成后,使用CMake进行编译: ``` # 进入g2o源代码目录 cd g2o # 创建build目录 mkdir build cd build # 使用CMake进行编译 cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_COMPILER=g++ -DG2O_BUILD_APPS=OFF -DG2O_BUILD_EXAMPLES=OFF -DG2O_USE_OPENGL=OFF -DEIGEN3_INCLUDE_DIR=<path/to/eigen> -DBOOST_ROOT=<path/to/boost> -DBUILD_SHARED_LIBS=OFF make -j4 # 编译完成后,生成g2o库文件和头文件 ``` 6. 生成Python编译完成后,可以使用Boost.Python将g2o库编译Python库。 在g2o库的build目录下,执行以下命令: ``` # 生成g2o的Pythoncmake --build . --target install ``` 执行完以上命令后,会在g2o库的build目录下生成一个dist目录,其中包含了生成Python库文件和相关头文件。 注意:在使用Python库时,需要将g2o库的路径添加到系统环境变量PYTHONPATH中。 以上是在Windows下使用CMake编译g2o库并生成Python库的步骤,希望对你有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值