[CMake notes] CMake add the source file and header files to your solution

 review

In a previous note in summary, when it comes, aux_source_directory this function when you add the source file, add the header file does not go in, look here mediated by another method, which I have been using.

Add files * .cpp and * .h

Folder structure

CMakeFile
  |--CMakeLists.txt
  |--main.cpp
  |--stdafx.h

CMakeLists.txt

1  # CMake the minimum version required
 2 cmake_minimum_required (VERSION 3.0 )
 3  
4  # Item Name
 5  Project (CMakeFile)
 6  
7  # Find all .cpp and .h files in the specified directory and place the variable name specified in SC_FILES
 8 FILE (GLOB SC_FILES " * .cpp "  " * .h " )
 . 9  
10  # specify generate a target
 . 11 add_executable ($ {$ {SC_FILES the PROJECT_NAME}})

Build the solution

 Advanced

In many cases, a third party from acquiring some of the features of the source files directly approach our own code can be placed in a folder, such as direct introduction of the above, but personally feel that this is not conducive to the management of these documents, especially some external source file special more time, will feel the entire folder too many files, and messy. I am more by function, divided in different folders under management, as I introduced md5 below the relevant code (github there):

Folder structure

CMakeFile
  |--common
  |  |--md5
  |     |--md5.cpp
  |     |--md5.h
  |--CMakeLists.txt
  |--main.cpp
  |--stdafx.h

Modify CMakeLists.txt

. 1  # CMake lowest version number in claim
 2 cmake_minimum_required (VERSION 3.0 )
 . 3  
. 4  # project name
 . 5  Project (CMakeFile)
 . 6  
. 7  # Set md5 code file path
 . 8 SET (md5_file " ./common/md5/md5.cpp "  " ./ the Common / MD5 / md5.h " )
 9  
10  # Find all .cpp and .h files in the specified directory and place the variable name specified in SC_FILES
 11 fILE (GLOB SC_FILES " * .cpp "  " * .h " )
 12  
13  # specify generate a target
 14 add_executable ($ {$ {SC_FILES the PROJECT_NAME}}} $ {md5_file)

Here you need to set a code file corresponding to the path, the path must take the last value added to the target generated.

Solution generation after

Here, you will find a problem, md5 code file that is already stored to achieve sub-folder management, but not in the solution in a packet Oh, my obsessive-compulsive disorder and had committed.

And then advanced, packet source solutions

In order to solve the above problems, cure my OCD, I went and looked at the manual CMake. The original script then just add a line in CMakeLists.txt, call a source_group function on the line:

. 1  # CMake lowest version number in claim
 2 cmake_minimum_required (VERSION 3.0 )
 . 3  
. 4  # project name
 . 5  Project (CMakeFile)
 . 6  
. 7  # Set md5 code file path
 . 8 SET (md5_file " ./common/md5/md5.cpp "  " ./ the Common / MD5 / md5.h " )
 9  
10  # Find all .cpp and .h files in the specified directory and place the variable name specified in SC_FILES
 11 fILE (GLOB SC_FILES " * .cpp "  " * .h " )
 12  
13  # of md5 md5 source packets to the group
 14  source_group (md5 md5_file the FILES $ {})
 15  
16 # Specify generate a target
 . 17 add_executable ($ {$ {SC_FILES the PROJECT_NAME}}} $ {md5_file)

After the re-generation solutions

 

It should be noted that other modifications CMakeList.txt after regenerate, VS will be prompted to reload the solution, but the call source_group function, the solution is not automatically loaded and needs to close VS reopen the project.

 

Guess you like

Origin www.cnblogs.com/dilex/p/11102152.html