CMake add_custom_command找不到文件。

huangapple go评论48阅读模式
英文:

CMake add_custom_command does not find file

问题

I have a example project with the structure below:

--SampleMinimal
----CMakeLists.txt
----main.cpp
----sample.cpp

and the CMakeLists.txt:

cmake_minimum_required(VERSION 3.20)
project(sample-minimal)
set(CMAKE_CXX_STANDARD 11)

add_custom_command(OUTPUT sample2.cpp
COMMAND cp sample.cpp sample2.cpp
DEPENDS sample.cpp)
add_executable(grpc-app-main main.cpp sample2.cpp)

While I build the target, I get these errors:

$cmake --S . -B build
$ cmake --build build
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/SL/Projects/cpp/sample_minimal/build
[ 25%] Generating sample2.cpp
cp: sample.cpp: No such file or directory
make[2]: *** [sample2.cpp] Error 1
make1: *** [CMakeFiles/grpc-app-main.dir/all] Error 2
make: *** [all] Error 2

There exists in the project, Why does it could not find the file, why doesn't it copy the sample into the cmake-build-debug folder like other source files?

英文:

I have a example project with the structure below:

--SampleMinimal
----CMakeLists.txt
----main.cpp
----sample.cpp

and the CMakeLists.txt:

cmake_minimum_required(VERSION 3.20)
project(sample-minimal)
set(CMAKE_CXX_STANDARD 11)

add_custom_command(OUTPUT sample2.cpp
        COMMAND cp sample.cpp sample2.cpp
        DEPENDS sample.cpp)
add_executable(grpc-app-main main.cpp sample2.cpp)

While I build the target, I get these errors:

$cmake --S . -B build
$ cmake --build build
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/SL/Projects/cpp/sample_minimal/build
[ 25%] Generating sample2.cpp
cp: sample.cpp: No such file or directory
make[2]: *** [sample2.cpp] Error 1
make[1]: *** [CMakeFiles/grpc-app-main.dir/all] Error 2
make: *** [all] Error 2

There exists in the project, Why does it could not find the file, why doesn't it copy the sample into the cmake-build-debug folder like other source files?

答案1

得分: 1

默认情况下,运行 add_custom_command 命令的工作目录是与当前的 CMakeLists.txt 对应的构建树中的目录。这意味着该命令无法通过相对路径 sample.cpp 找到文件,并且不会将文件复制到可由可执行目标找到的位置,命名为 sample2.cpp

生成文件到源目录通常不是一个好选择,因为它会阻止您根据相同源目录设置多个构建目录,以确保所有项目都能正常工作。在这种情况下,这不会引起问题,因为生成文件的逻辑与配置无关。

这是我会使用的方式:

set(GENERATED_SOURCE ${CMAKE_CURRENT_BINARY_DIR}/sample2.cpp)

add_custom_command(OUTPUT sample2.cpp
        COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/sample.cpp ${GENERATED_SOURCE}
        DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/sample.cpp)
add_executable(grpc-app-main main.cpp ${GENERATED_SOURCE})

注意:我还更改了使用 cp 到使用 cmake -E copy,以使其在各个平台上都能正常工作。

英文:

The default working directory for running the command of add_custom_command is the directory in the build tree corresponding to the current CMakeLists.txt. This means the command isn't be able to find the file via the relative path sample.cpp and it wouldn't copy the file to a location where it can be found by the executable target as sample2.cpp.

Generating files to the source tree is not a good choice anyways, since it prevents you from setting up multiple build dirs based on the same source dir in a way that has all of the projects work properly. (In this case it happens to not cause any problems, since the logic for generating the file is independent of the configuration.)

Here's what I'd use

set(GENERATED_SOURCE ${CMAKE_CURRENT_BINARY_DIR}/sample2.cpp)

add_custom_command(OUTPUT sample2.cpp
        COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/sample.cpp ${GENERATED_SOURCE}
        DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/sample.cpp)
add_executable(grpc-app-main main.cpp ${GENERATED_SOURCE})

Note: I also changed the use of cp to using cmake -E copy to be platform independent here.

答案2

得分: 0

If sample.cpp isn't in /Users/AAA/clion/BigPro/cmake-build-debug/grpc-demo, you either have to adjust the WORKING_DIRECTORY of the custom command, or use absolute paths for the files. For example, instead of COMMAND cp sample.cpp sample2.cpp, do COMMAND cp "${PROJECT_SOURCE_DIR}/sample.cpp" sample2.cpp.

英文:

If sample.cpp isn't in /Users/AAA/clion/BigPro/cmake-build-debug/grpc-demo, you either have to adjust the WORKING_DIRECTORY of the custom command, or use absolute paths for the files. For example, instead of COMMAND cp sample.cpp sample2.cpp, do COMMAND cp "${PROJECT_SOURCE_DIR}/sample.cpp" sample2.cpp.

huangapple
  • 本文由 发表于 2023年5月25日 11:44:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76328787.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定