1. sdsf(single direction single file)
1.1 The directory tree
/* ./template | +--- build | +---main.cpp | +---CMakeLists.txt */
1.2 Sources code
// main.cpp #include <iostream> using 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 build 2 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 <iostream> #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 <iostream> 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
// CMakeLists cmake_minimum_required(VERSION 2.6) project(template) aux_source_directory(./ allfiles) // The current dir add_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 <iostream> #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 src aux_source_directory(./ AllfilesInSrc) add_library(SrcFile ${AllfilesInSrc}) //CMakeLists in template cmake_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 build 2 cmake .. 3 make 4 5 ./template 6 7 I'm in a.cpp 8 I'm in file b 9 Hello, world!