Makefile

Hi guys, I'm introducing myself to classes in c++. Here in this example, I 've created a class "particle" ( which contains as protected data members the mass and the charge of a particle ) and several derived classes of known particle ( electron.h, proton.h, ecc. ), after that I put all this known particles in a proper directory.
How can I include the path of that directory in the Makefile instead of including it in the main.cxx?
Thanks in advance for the help.
I leave here my current code.

main.cxx :

1
2
3
4
5
6
7
8
9
#include "particle.h"
#include "./known_part/electron.h"

#include<iostream>

int main () {
   ...
   return 0;
}


Makefile :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
  CXXFLAGS = -Wall --pedantic -g -std=c++11

ex: main.o particle.o 
	g++ -o $@ main.o particle.o 

main.o: main.cxx 
	g++ -c main.cxx ${CXXFLAGS} 

particle.o: particle.cxx particle.h
	g++ -c particle.cxx ${CXXFLAGS}

clean:
	rm *.o
The g++ takes flag -I, so you could add -Iknown_part


However, check CMake: https://cmake.org/cmake/help/latest/guide/tutorial/index.html

The CMake writes a Makefile for you and supports out of source builds.
In other words, you compile is different directory than where your source code is.
That makes cleaning easier; you can simply remove the build directory.
thanks mate, I will check it!
Topic archived. No new replies allowed.