mkdir build cd build cmake ..
Сборка проекта
make
или в общем случае
cmake --build . --parallel
#include <iostream> int main(int argc, char** argv) { std::cout << "Hello, World!" << std::endl; return 0; }
cmake_minimum_required(VERSION 3.20) project(hello) add_executable(main main.cpp)
#include "foo.h" int main(int argc, char** argv) { hello_world(); return 0; }
void hello_world();
#include <iostream> void hello_world() { std::cout << "Hello, World!" << std::endl; }
cmake_minimum_required(VERSION 3.20) project(hello_world) set(SOURCE_EXE main.cpp) set(SOURCE_LIB foo.cpp) # STATIC/SHARED add_library(src_lib STATIC ${SOURCE_LIB}) add_executable(main ${SOURCE_EXE}) target_link_libraries(main src_lib)
#include "foo.h" int main(int argc, char** argv) { hello_world(); return 0; }
void hello_world();
#include <iostream> void hello_world() { std::cout << "Hello, World!" << std::endl; }
cmake_minimum_required(VERSION 3.20) project(foo) set(SOURCE_LIB foo.cpp) # build "foo" library add_library(foo STATIC ${SOURCE_LIB})
cmake_minimum_required(VERSION 3.20) project(hello_world) set(SOURCE_EXE main.cpp) include_directories(foo) add_executable(main ${SOURCE_EXE}) add_subdirectory(foo) target_link_libraries(main foo)
#include "foo.h" #include "lib.h" int main(int argc, char** argv) { hello_world(); hello_lib(argc, argv); return 0; }
void hello_world();
#include <iostream> void hello_world() { std::cout << "Hello, World!" << std::endl; }
void hello_lib(int, char**);
#include <boost/program_options/options_description.hpp> #include <boost/program_options/option.hpp> #include <boost/program_options/variables_map.hpp> #include <boost/program_options/parsers.hpp> #include <iostream> namespace po = boost::program_options; void hello_lib(int argc, char** argv) { po::options_description desc("Allowed options"); desc.add_options() ("help, h", "This is help message") ; po::variables_map vm; try { po::store(po::parse_command_line(argc, argv, desc), vm); po::notify(vm); } catch (const boost::wrapexcept<po::unknown_option>& e) { std::cerr << "Error: Unknown option: " << e.get_option_name() << std::endl; std::cerr << "Use --help for usage information." << std::endl; } if (vm.count("help")) { std::cout << desc << "\n"; } }
cmake_minimum_required(VERSION 3.20) project(hello_world) find_package( Boost 1.40 COMPONENTS program_options REQUIRED ) include_directories( ${Boost_INCLUDE_DIR} ) set(SOURCE_EXE main.cpp) set(SOURCE_LIB foo.cpp lib.cpp) add_library(src_lib STATIC ${SOURCE_LIB}) add_executable(main ${SOURCE_EXE}) target_link_libraries(main src_lib ${Boost_LIBRARIES})
#include "foo.h" #include "lib.h" int main(int argc, char** argv) { hello_world(); hello_lib(argc, argv); return 0; }
void hello_world();
#include <iostream> void hello_world() { std::cout << "Hello, World!" << std::endl; }
cmake_minimum_required(VERSION 3.20) project(foo) set(SOURCE_LIB foo.cpp) # build "foo" library add_library(foo STATIC ${SOURCE_LIB})
void hello_lib(int, char**);
#include <boost/program_options/options_description.hpp> #include <boost/program_options/option.hpp> #include <boost/program_options/variables_map.hpp> #include <boost/program_options/parsers.hpp> #include <iostream> namespace po = boost::program_options; void hello_lib(int argc, char** argv) { po::options_description desc("Allowed options"); desc.add_options() ("help, h", "This is help message") ; po::variables_map vm; try { po::store(po::parse_command_line(argc, argv, desc), vm); po::notify(vm); } catch (const boost::wrapexcept<po::unknown_option>& e) { std::cerr << "Error: Unknown option: " << e.get_option_name() << std::endl; std::cerr << "Use --help for usage information." << std::endl; } if (vm.count("help")) { std::cout << desc << "\n"; } }
cmake_minimum_required(VERSION 3.20) project(hello_world) find_package( Boost 1.40 COMPONENTS program_options REQUIRED ) include_directories( ${Boost_INCLUDE_DIR} ) include_directories(foo) add_subdirectory(foo) set(SOURCE_EXE main.cpp) set(SOURCE_LIB lib.cpp) add_library(src_lib STATIC ${SOURCE_LIB}) add_executable(main ${SOURCE_EXE}) target_link_libraries(main src_lib foo ${Boost_LIBRARIES})
#include "foo.h" #include "lib.h" int main(int argc, char** argv) { hello_world(); hello_lib(argc, argv); return 0; }
void hello_world();
#include <iostream> void hello_world() { std::cout << "Hello, World!" << std::endl; }
cmake_minimum_required(VERSION 3.20) project(foo) set(SOURCE_LIB foo.cpp) # build "foo" library add_library(foo STATIC ${SOURCE_LIB})
void hello_lib(int, char**);
#include <boost/program_options/options_description.hpp> #include <boost/program_options/option.hpp> #include <boost/program_options/variables_map.hpp> #include <boost/program_options/parsers.hpp> #include <iostream> namespace po = boost::program_options; void hello_lib(int argc, char** argv) { po::options_description desc("Allowed options"); desc.add_options() ("help, h", "This is help message") ; po::variables_map vm; try { po::store(po::parse_command_line(argc, argv, desc), vm); po::notify(vm); } catch (const boost::wrapexcept<po::unknown_option>& e) { std::cerr << "Error: Unknown option: " << e.get_option_name() << std::endl; std::cerr << "Use --help for usage information." << std::endl; } if (vm.count("help")) { std::cout << desc << "\n"; } }
cmake_minimum_required(VERSION 3.20) project(lib) find_package( Boost 1.40 COMPONENTS program_options REQUIRED ) include_directories( ${Boost_INCLUDE_DIR} ) set(SOURCE_LIB lib.cpp) # build "lib" library add_library(lib STATIC ${SOURCE_LIB}) target_link_libraries(lib ${Boost_LIBRARIES})
cmake_minimum_required(VERSION 3.20) project(hello_world) include_directories(foo) add_subdirectory(foo) include_directories(lib) add_subdirectory(lib) set(SOURCE_EXE main.cpp) add_executable(main ${SOURCE_EXE}) target_link_libraries(main foo lib)
#include <QCoreApplication> #include <QDebug> int main(int argc, char** argv) { QCoreApplication app(argc, argv); qDebug() << "Hello, World"; return app.exec(); }
cmake_minimum_required(VERSION 3.20) project(qt_hello_world) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) set(CMAKE_AUTOUIC ON) set(CMAKE_PREFIX_PATH "/opt/Qt/6.4.1/gcc_64/") find_package(Qt6 REQUIRED COMPONENTS Core) set(SOURCE_EXE main.cpp) add_executable(main ${SOURCE_EXE}) target_link_libraries(main PRIVATE Qt6::Core)
#include <QCoreApplication> #include "lib/lib.h" int main(int argc, char** argv) { QCoreApplication app(argc, argv); qt_hello(); return app.exec(); }
#include <QDebug> void qt_hello() { qDebug() << "Hello, World"; }
void qt_hello();
cmake_minimum_required(VERSION 3.20) project(qt_hello) # Find Qt6 components required for this module find_package(Qt6 REQUIRED COMPONENTS Core) set(SOURCE_LIB lib.cpp) add_library(lib STATIC ${SOURCE_LIB}) target_link_libraries(lib PRIVATE Qt6::Core)
cmake_minimum_required(VERSION 3.20) project(qt_hello_world) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) set(CMAKE_AUTOUIC ON) set(CMAKE_PREFIX_PATH "/opt/Qt/6.4.1/gcc_64/") find_package(Qt6 REQUIRED COMPONENTS Core) set(SOURCE_EXE main.cpp) add_subdirectory(lib) add_executable(main ${SOURCE_EXE}) target_link_libraries(main PRIVATE lib PRIVATE Qt6::Core)
—
По материалам: