0

Here's my problem : I have a makefile file containing the info to compile everything in my project to make my life easier, but it is giving me some headaches recently.

It can compile multiple files, such as this :

objects/io.o: sources/io.cpp
    @g++ -c $< -o $@ -std=c++11
objects/map.o: sources/map.cpp
    @g++ -c $< -o $@ -std=c++11

At the top of the makefile, I have variables declared as such :

IO="objects/io.o"
MAP="objects/map.o"
[... other object files ...]
ALL="$(IO) $(MAP) [...]"

When I want to compile my main file, I use this command :

main.exe: tests/main.cpp
    @g++ $< $(ALL) -o $@ -std=c++11

When I compile this problem manually (inputting everything in one line of command, instead of making make main.exe) it compiles without problems.

However, when I use the make command for the project, the following error pops up :

clang: error: no such file or directory: 'objects/io.o objects/map.o [...]'
make: *** [main.exe] Error 1

Can I not use variables this way ? I'm extremely confused. I know for a fact those files are compiled, it just seems the make utility doesn't understand file paths.

2
  • 1
    Remove all quotes. Commented Apr 28, 2017 at 13:04
  • I'm dumb. Thanks for the help ! Commented Apr 28, 2017 at 13:08

1 Answer 1

1

I think the problem is that you have quoted your variables. Try unquoting them and they won't expand a single parameter:

ALL = $(obj1) $(obj2) ...

Also, for those objects which use the same compilation process I generally define a single rule:

obj/%.o: %.cc
    $(GCC) -o $@ -c $< $(FLAGS)

Obviously that would require defining the extra variables GCC and FLAGS

Sign up to request clarification or add additional context in comments.

2 Comments

Quick question, does your overall command (the obj/%.o: %.cc ) recompiles every .o file everytime or does it only compiles the modified files ?
It only compiles object files when it's asked to. It will follow the same semantics as if you had manually specified each one. The way I typically use this is by requiring all the object files as a dependency of a library or the main executable. i.e. main.exe: main.cc obj/obj1.o obj/obj2.o .... Of course that can be shortened by putting all your objects into a variable

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.