samedi 8 février 2020

How to write makefile for a target that includes a header file?

I have two files "create-exercise.cpp" and "exercise.hpp". I want to write a makefile to use gnu++17 and g++ to compile them. exercise.hpp is included in create-exercise.cpp. I only want to get a binary out of create-exercise.cpp. the command I would use is g++ -std=gnu++17 create-exercise.cpp -o create-exercise and it works well. I tried using the following in a make file.

CXXFLAGS=-Wall -std=gnu++17

create-exercise: create-exercise.cpp exercise.hpp

but that generated the following g++ -Wall -std=gnu++17 create-exercise.cpp exercise.hpp -o create-exercise I don't want exercise.hpp to be included in the compilation command. I also tried to use the following instead.

CXXFLAGS=-Wall -std=gnu++17

create-exercise.o: create-exercise.cpp exercise.hpp

That generated g++ -Wall -std=gnu++17 -c -o create-exercise.o create-exercise.cpp. I don't want the -c flag. because when I try to run create-exercise.o that results in permission denied error. I tried the following as well:

CXXFLAGS=-Wall -std=gnu++17

create-exercise.o: create-exercise.cpp exercise.hpp
    $(CXX) $(CXXFLAGS) create-exercise.cpp -o create-exercise.o

but when I edit exercise.hpp make says 'create-exercise.o' is up to date. and doesn't recompile it. what should I do?

Aucun commentaire:

Enregistrer un commentaire