1. sdsf(single direction single file)
1.1 The directory tree
/*./template | +--- build | +---main.cpp | +---CMakeLists.txt */
1.2 Sources code
// main.cpp #includeusing namespace std; int main(int argc, char **argv) { std::cout << "Hello, world!" << std::endl; return 0; } //CMakeLists.txt cmake_minimum_required(VERSION 2.6) //------The minimum version required---// project(template) //------The project information-----// add_executable(template main.cpp) //-----Build Target------// install(TARGETS template RUNTIME DESTINATION bin)
1.3 Compile All
1 cd build2 cmake .. 3 make
2. Single direct Multi- files
2.1 The directory tree
/* ./template | +--- build/ | +---main.cpp | +---a.cpp | +---CMakeLists.txt*/
2.2 Sources Code
// main.cpp #include#include "a.h"using namespace std;int main(int argc, char **argv) { display(); std::cout << "Hello, world!" << std::endl; return 0;}// a.cpp#include "a.h"using namespace std;void display(void){ cout << "I'm in a.cpp"<< endl; }// a.h#ifndef A_H_#define A_H_#include void display(void);#endif //CMakeLists.txt cmake_minimum_required(VERSION 2.6) //------The minimum version required---// project(template) //------The project information-----// add_executable(template main.cpp a.cpp) //-----Build Target------// install(TARGETS template RUNTIME DESTINATION bin)
only add a.cpp in add_executable ,and there is a accepted way----used command aux_source_directory
// CMakeListscmake_minimum_required(VERSION 2.6)project(template)aux_source_directory(./ allfiles) // The current diradd_executable(template ${allfiles})install(TARGETS template RUNTIME DESTINATION bin)
3. Multi dir Multi files
3.1 dir tree
/* ./template | +--- build/ | +---src/ | +---b.cpp | +---CMakeLists | +---main.cpp | +---a.cpp a.h | +----b.h | +---CMakeLists.txt*/
3.2 Sources Code
// main.cpp#include#include "a.h"#include "b.h"using namespace std;int main(int argc, char **argv) { display(); DisplayInFileb(); std::cout << "Hello, world!" << std::endl; return 0;}//a.cpp#include "a.h"using namespace std;void display(void){ cout << "I'm in a.cpp"<< endl; }// b.cpp#include "b.h"using namespace std;void DisplayInFileb(void){ cout << "I'm in file b" << endl;}// CMakeLists in srcaux_source_directory(./ AllfilesInSrc)add_library(SrcFile ${AllfilesInSrc})//CMakeLists in templatecmake_minimum_required(VERSION 2.6)project(template)aux_source_directory(./ allfiles)add_subdirectory(src)add_executable(template ${allfiles})target_link_libraries(template SrcFile)install(TARGETS template RUNTIME DESTINATION bin)
3.3 compile all
1 cd build2 cmake ..3 make4 5 ./template6 7 I'm in a.cpp8 I'm in file b9 Hello, world!