简体   繁体   中英

Project Linking and Compiling files

I wanna start building a project and I have the following folder structure(at least, a part of it):

lib
|---class1.cpp
|---class1.hpp
src
|---main.cpp

I have the MinGW compiler and I don't know how to compile all .cpp files. I know the command g++ *.cpp -o main for compiling all the files, but works only for files in the same folder.

Should I move all my files to the src folder? Should I change the project structure? Also, I'm really doubtful if I should use CMake or not.

For a barebones project, your structure is fine. Just add the following CMakeLists.txt file to the root of your directory:

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)

Your directory should now look like

CMakeLists.txt
lib
|---class1.cpp
|---class1.hpp
src
|---main.cpp

Then to build the project, you will typically

  1. Make a folder where you build everything (often called build , but it's up to you). Now the directory looks like

     CMakeLists.txt lib |---class1.cpp |---class1.hpp src |---main.cpp build
  2. Go into the build folder and on the command like, configure your project with the command cmake .. (just to reiterate... this needs to be done from inside the build folder).

  3. Build your project with the make command (again from inside the build folder).

After that, you should have an executable called cool_exe in the build folder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM