[英]Project Linking and Compiling files
我想开始构建一个项目,并且我有以下文件夹结构(至少是其中的一部分):
lib
|---class1.cpp
|---class1.hpp
src
|---main.cpp
我有 MinGW 编译器,但我不知道如何编译所有.cpp
文件。 我知道命令g++ *.cpp -o main
用于编译所有文件,但仅适用于同一文件夹中的文件。
我应该将所有文件移动到src
文件夹吗? 我应该改变项目结构吗? 另外,我真的很怀疑我是否应该使用 CMake。
对于准系统项目,您的结构很好。 只需将以下 CMakeLists.txt 文件添加到目录的根目录:
cmake_minimum_required(VERSION 3.5)
# Given your project a descriptive name
project(cool_project)
# CHoose whatever standard you want here... 11, 14, 17, ...
set(CMAKE_CXX_STANDARD 14)
# The first entry is the name of the target (a.k.a. the executable that will be built)
# every entry after that should be the path to the cpp files that need to be built
add_executable(cool_exe src/main.cpp lib/class1.cpp)
# Tell the compiler where the header files are
target_link_libraries(cool_exe PRIVATE lib)
你的目录现在应该看起来像
CMakeLists.txt
lib
|---class1.cpp
|---class1.hpp
src
|---main.cpp
然后要构建项目,您通常会
创建一个文件夹,您可以在其中构建所有内容(通常称为build
,但这取决于您)。 现在目录看起来像
CMakeLists.txt lib |---class1.cpp |---class1.hpp src |---main.cpp build
进入构建文件夹,然后在命令上,使用命令cmake ..
配置您的项目(只是重申......这需要从构建文件夹内部完成)。
使用make
命令构建您的项目(再次从构建文件夹中)。
之后,您应该在 build 文件夹中有一个名为cool_exe
的可执行文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.