我新编写了一个叫path.cpp的文件,和其他原来的文件都放在了example文件夹下需要改cmakelist中的这个地方吗
时间: 2024-02-21 17:58:14 浏览: 10
是的,如果您在 example 文件夹下新建了一个名为 path.cpp 的源代码文件,您需要在 CMakeLists.txt 文件中添加 path.cpp 文件,才能将其编译成可执行文件。具体来说,您需要在 add_executable 命令中添加 path.cpp 文件,例如:
```
add_executable(example path.cpp file1.cpp file2.cpp)
```
其中,example 是可执行文件的名称,path.cpp、file1.cpp 和 file2.cpp 分别是源代码文件的名称。注意,在添加 path.cpp 文件之前,您需要确认该文件的路径和名称是否正确。
相关问题
ubuntu中,我新编写了一个叫path.cpp的文件,和其他原来的文件都放在了example文件夹下,重新编译时报错:No SOURCES given to target,我觉得应该是cmakelist中的这句有问题add_executable(${example} ${example}.cpp ${xCoreSDK-examples_SOURCE_DIR}/cpp/print_helper.hpp)
是的,你的猜测是正确的。根据错误信息,CMake无法找到要编译的源文件。你需要将路径和文件名传递给`add_executable`命令。假设你的`path.cpp`文件位于`example`文件夹下,你需要将`${example}.cpp`改为`example/path.cpp`。修改后的命令应该是这样的:
```
add_executable(${example} example/path.cpp ${xCoreSDK-examples_SOURCE_DIR}/cpp/print_helper.hpp)
```
这样CMake就能找到你的源文件并编译它了。
.cpp .h不同文件夹下 cmakelist
当.cpp和.h文件位于不同文件夹下时,我们需要在CMakeLists.txt中正确配置来确保编译器能够正确找到这些文件。
首先,在CMakeLists.txt文件中,我们需要添加以下代码:
```cmake
# 指定.cpp文件所在路径
include_directories(${CMAKE_SOURCE_DIR}/path/to/cpp)
# 指定.h文件所在路径
include_directories(${CMAKE_SOURCE_DIR}/path/to/header)
# 添加.cpp文件
add_executable(MyProgram ${CMAKE_SOURCE_DIR}/path/to/cpp/myprogram.cpp)
# 添加其他.cpp文件(如果有的话)
add_executable(OtherProgram ${CMAKE_SOURCE_DIR}/path/to/cpp/otherprogram.cpp)
# 链接头文件和.cpp文件
target_link_libraries(MyProgram MyHeader)
target_link_libraries(OtherProgram MyHeader)
```
其中,`${CMAKE_SOURCE_DIR}`表示CMakeLists.txt所在路径的上层路径。我们需要将路径中的`path/to/cpp`替换为实际的文件夹路径。
在这个例子中,我们假设我们的程序有两个.cpp文件:myprogram.cpp和otherprogram.cpp,它们的.h文件分别是MyHeader.h和OtherHeader.h。
我们首先使用`include_directories`命令指定.h文件所在的文件夹路径,然后使用`add_executable`命令添加.cpp文件。最后,使用`target_link_libraries`命令链接头文件和.cpp文件。
通过这样的配置,CMake就能够正确找到.cpp和.h文件,编译我们的程序。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)