如何在另一个文件夹中添加源文件 [英] How to add source files in another folder
问题描述
我正在使用cmake在C ++中构建项目。假设我的源文件夹中有以下目录
源
| _Dir1
| | _Class.cpp
| | _Class.hpp
|
| _Dir2
| _Main.cpp
在Dir1中有一类头文件和实现文件(Class.cpp和Class.hpp)。
在Dir2中,有一个主要应用程序在Dir1中使用该类
告诉Dir2中的CMakeList用Dir1 / Class.cpp文件生成可执行文件的好方法是什么?
编辑:更具体地说,我想定义Class.cpp的源文件必须在Dir1的CMakeLists.txt中使用,而不在Dir2的中使用。以另一种方式进行操作对我来说显然是错误的,并且很难使用,因此,如果有理由他们强迫我对此做一些澄清,那就太好了。
我目前正在做的是在Dir2 / CMakeLists.txt中对Class.cpp文件位置进行硬编码,但是当我有很多类一起交互时,这只是无法缩放。
假设您在来源处只有一个
目录,您将使用不同的 CMakeLists.txt
文件 file()
命令
file(GLOB Dir1_Sources相对 Dir1 * .cpp)
文件(GLOB Dir2_Sources相对 Dir2 * .cpp)
并将 file()
命令生成的两个集合都添加到目标的源列表中:
add_executable(MyProgram $ {Dir1_Sources} $ {Dir2_Sourc es})
或者您可以放置<$ Dir1
和 Dir2
(主)下的c $ c> CMakeLists.txt 文件(主)看起来像跟随
来源
|
| _ CMakeLists.txt
| >项目(MyProgram)
| > cmake_minimum_required(版本3.8)
| > add_subdirectory( Dir1)
| > add_subdirectory( Dir2)
|
| _ Dir1
| | _ CMakeLists.txt
| >文件(GLOB源 * .cpp)
| > add_library(Dir1 STATIC $ {Sources})
| _ Dir2
| _ CMakeLists.txt
>文件(GLOB源 * .cpp)
> add_executable(MyProgram $ {Sources})
> target_link_libraries(MyProgram Dir1)
添加子目录作为链接到主要目标的其他(静态)库。 / p>
I'm using cmake to build my project in C++. Assume I have the following directories on my Source folder
Source
|_Dir1
| |_Class.cpp
| |_Class.hpp
|
|_Dir2
|_Main.cpp
In Dir1 there's a class with its header and implementation files (Class.cpp and Class.hpp).
In Dir2 there's the main application which uses the class in Dir1
What is the good way to tell the CMakeLists in Dir2 to build the executable with Dir1/Class.cpp file?
EDIT: To be more specific, I want to define that the source file for Class.cpp has to be used in Dir1's CMakeLists.txt, and not in Dir2's. Doing it the other way feels plain wrong to me and it's hard to use, so if there's a reason they're enforcing me to do this some clarification on the topic would be nice.
What I'm currently doing is hard-coding the Class.cpp file location in Dir2/CMakeLists.txt but that just doesn't scale when I've got a bunch of classes interacting together.
Supposed you have a single CMakeLists.txt
file at the Source
directory, you'll create two variables using different file()
commands
file(GLOB Dir1_Sources RELATIVE "Dir1" "*.cpp")
file(GLOB Dir2_Sources RELATIVE "Dir2" "*.cpp")
and add both sets generated by the file()
commands to your target's source list:
add_executable(MyProgram ${Dir1_Sources} ${Dir2_Sources})
Alternatively you can place a CMakeLists.txt
file under Dir1
and Dir2
(Main) looking as follows
Source
|
|_ CMakeLists.txt
| > project(MyProgram)
| > cmake_minimum_required(VERSION 3.8)
| > add_subdirectory("Dir1")
| > add_subdirectory("Dir2")
|
|_ Dir1
| |_ CMakeLists.txt
| > file(GLOB Sources "*.cpp")
| > add_library(Dir1 STATIC ${Sources})
|_ Dir2
|_ CMakeLists.txt
> file(GLOB Sources "*.cpp")
> add_executable(MyProgram ${Sources})
> target_link_libraries(MyProgram Dir1)
to add subdirectories as further (static) libraries linked to your main target.
这篇关于如何在另一个文件夹中添加源文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!