在使用cmake构建的项目中添加头文件和.cpp文件

时间:2021-01-10 15:02:55

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构建的,我不能?

3 个解决方案

#1


9  

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:

通过这种方式,您可以执行以下两种方法之一,将新添加的标头/源添加到VS中:

  1. need to generate in CMake again.
  2. 需要再次在CMake中生成。
  3. 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.
  4. 伪造一点点编辑CMakeLists.txt,例如只需添加一个空格。然后在VS中构建您的解决方案,它将自动添加新的头/源文件。

#2


2  

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中重新加载。

#3


0  

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文件。 Althogh它可能不是你的需要,它可能对其他看到它的人有用。

#1


9  

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:

通过这种方式,您可以执行以下两种方法之一,将新添加的标头/源添加到VS中:

  1. need to generate in CMake again.
  2. 需要再次在CMake中生成。
  3. 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.
  4. 伪造一点点编辑CMakeLists.txt,例如只需添加一个空格。然后在VS中构建您的解决方案,它将自动添加新的头/源文件。

#2


2  

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中重新加载。

#3


0  

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文件。 Althogh它可能不是你的需要,它可能对其他看到它的人有用。