Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding header and .cpp files in a project built with cmake

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?

like image 333
mariosbikos Avatar asked May 01 '15 11:05

mariosbikos


People also ask

How do I add a header to CMake?

To include headers in CMake targets, use the command target_include_directories(...) . Depending on the purpose of the included directories, you will need to define the scope specifier – either PUBLIC , PRIVATE or INTERFACE .

Can I include CPP file in header?

You make the declarations in a header file, then use the #include directive in every . cpp file or other header file that requires that declaration. The #include directive inserts a copy of the header file directly into the . cpp file prior to compilation.


3 Answers

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:

  1. need to generate in CMake again.
  2. 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.
like image 74
herohuyongtao Avatar answered Oct 05 '22 06:10

herohuyongtao


you need to add every .h and .cpp file to CMakeList.txt like this:

# 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.

like image 45
rashmatash Avatar answered Oct 05 '22 06:10

rashmatash


Although it's 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. Although it may not be your need, it may be useful for other peoples who see it.

like image 39
Dharma Avatar answered Oct 05 '22 08:10

Dharma