C++ 在使用 cmake 构建的项目中添加头文件和 .cpp 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29986353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding header and .cpp files in a project built with cmake
提问by mariosbikos
I have built a project using cmake and some libraries.I want however to add some header and .cpp files in the project which I am going to code.What is the easiest way to do it?Can I just create a .cpp and header files and then build again project in Visual Studio? Or due to the fact that project was built using cmake I can't?
我已经使用 cmake 和一些库构建了一个项目。但是我想在我要编码的项目中添加一些头文件和 .cpp 文件。最简单的方法是什么?我可以只创建一个 .cpp 和头文件吗?文件,然后在 Visual Studio 中再次构建项目?或者由于项目是使用 cmake 构建的,我不能?
回答by herohuyongtao
You can put all header/source files in the same folder and use something like
您可以将所有头文件/源文件放在同一个文件夹中,并使用类似
file(GLOB SOURCES
header-folder/*.h
source-folder/*.cpp
)
add_executable(yourProj ${SOURCES})
In this way, you can do either of the following two methods to add new added header/source into VS:
这样,您可以通过以下两种方法之一将新添加的 header/source 添加到 VS 中:
- need to generate in CMake again.
- fake to edit the
CMakeLists.txt
a little bit, e.g. simply add a space. And then build your solution in VS, it will automatically add new header/source files.
- 需要再次在 CMake 中生成。
- 假编辑
CMakeLists.txt
一点点,例如简单地添加一个空格。然后在 VS 中构建您的解决方案,它会自动添加新的头文件/源文件。
回答by rashmatash
you need to add every .h
and .cpp
file to CMakeList.txt like this:
您需要像这样将每个.h
和.cpp
文件添加到 CMakeList.txt 中:
# Local header files here ONLY
SET(TARGET_H
Header.h
Plugin.h
messagelog.h
win32application.h
timer.h
)
# Local source files here
SET(TARGET_SRC
Plugin.cpp
messagelog.cpp
win32application.cpp
timer.cpp
)
then configure and build the solution again and reload it in VS.
然后再次配置和构建解决方案并在 VS 中重新加载它。
回答by Dharma
Although its a late Response and I just saw it. I am using CLion IDE from JetBrains, which adds these header and .cpp files automatically when you create them. Althogh it may not be your need, It may be a useful for other peoples who see it.
虽然它是一个迟到的响应,我刚刚看到它。我正在使用 JetBrains 的 CLion IDE,它会在您创建这些头文件和 .cpp 文件时自动添加它们。尽管它可能不是您的需要,但它可能对看到它的其他人有用。