andrei81
andrei81

Reputation: 68

Project Linking and Compiling files

I want to start building a project and I have the following folder structure:

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.

FINAL: I decided to go with CMake which made my life easier.

Upvotes: 0

Views: 136

Answers (1)

bremen_matt
bremen_matt

Reputation: 7349

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.

Upvotes: 1

Related Questions