如何从 CMake 中的另一个目录引用可执行文件?
How to refer executable from another directory in CMake?
我的项目结构:
/external-source-generating-tool
/external-source-generating-tool/CMakeLists.txt
/external-source-generating-tool/*.cpp
/src
/src/CMakeLists.txt
/src/*.cpp
/CMakeLists.txt
CMakeLists.txt:
add_subdirectory(external-source-generating-tool)
add_subdirectory(src)
/src/CMakeLists.txt:
add_custom_command(OUTPUT generated-source.cpp
COMMAND external-source-generating-tool -o generated-source.cpp
MAIN_DEPENDENCY external-source-generating-tool
COMMENT "Generating...")
add_executable(my-app source1.cpp generated-source.cpp)
问题是/src/CMakeLists.txt
找不到external-source-generating-tool
:
Error:Cannot find source file:
external-source-generating-tool
Tried extensions .c .C .c++ .cc .cpp .cxx .m .M .mm .h .hh .h++ .hm .hpp
.hxx .in .txx
如何从 /src/CMakeLists.txt
正确调用 external-source-generating-tool
?
问题出在 add_custom_command
的 MAIN_DEPENDENCY external-source-generating-tool
参数上。我将其替换为 DEPENDS external-source-generating-tool
:
add_custom_command(OUTPUT generated-source.cpp
COMMAND external-source-generating-tool -o generated-source.cpp
DEPENDS external-source-generating-tool
COMMENT "Generating...")
现在一切正常。
我的项目结构:
/external-source-generating-tool
/external-source-generating-tool/CMakeLists.txt
/external-source-generating-tool/*.cpp
/src
/src/CMakeLists.txt
/src/*.cpp
/CMakeLists.txt
CMakeLists.txt:
add_subdirectory(external-source-generating-tool)
add_subdirectory(src)
/src/CMakeLists.txt:
add_custom_command(OUTPUT generated-source.cpp
COMMAND external-source-generating-tool -o generated-source.cpp
MAIN_DEPENDENCY external-source-generating-tool
COMMENT "Generating...")
add_executable(my-app source1.cpp generated-source.cpp)
问题是/src/CMakeLists.txt
找不到external-source-generating-tool
:
Error:Cannot find source file:
external-source-generating-tool
Tried extensions .c .C .c++ .cc .cpp .cxx .m .M .mm .h .hh .h++ .hm .hpp .hxx .in .txx
如何从 /src/CMakeLists.txt
正确调用 external-source-generating-tool
?
问题出在 add_custom_command
的 MAIN_DEPENDENCY external-source-generating-tool
参数上。我将其替换为 DEPENDS external-source-generating-tool
:
add_custom_command(OUTPUT generated-source.cpp
COMMAND external-source-generating-tool -o generated-source.cpp
DEPENDS external-source-generating-tool
COMMENT "Generating...")
现在一切正常。