mardi 10 mars 2020

makefile with files from different folders (for C++)

I'm trying to compile a C++ test file, which is supposed to compile from files that are in adjacent folders in the project file structure. I have the following:

Project/TestFiles/makefile
Project/TestFiles/test.cpp
Project/OtherFiles/my_stuff.cpp
Project/OtherFiles/my_stuff.hpp

For the compile, I'm trying to leave the my_stuff.o file in the OtherFiles folder, so if I have other makefiles, they dont have to recompile separate versions each.

My makefile looks as follows:

CC = g++
CFLAGS = -std=c++11 -Wall -Wcomment -Werror -Wextra -Weffc++ -pedantic

run: test.out

test.out: test.cpp ../OtherFiles/my_stuff.o
  $(CC) $(CFLAGS) $^ -o $@

my_stuff.o: ../OtherFiles/my_stuff.cpp ../OtherFiles/my_stuff.hpp
  $(CC) $(CFLAGS) -c $<

I thought for a while that this setup worked, but then I started getting some weird problems and couldn't compile. For instance, having a static const map produced error: expected ';' after top level declarator. At first, Internet seemed to indicate that Mac compilers sometimes aren't able to compile static const maps with member initialisation lists (which it also complained about, if I removed the static const part). However, when I commented out everything to do with the std::map (leaving the makefile as described above) OR placed all files in the same folder (rewriting both the makefile as well as the #includes in test.cpp), everything is ok, but I'd like to use both std::maps AND the chosen file structure. Oh, and removing the extra warning flags doesn't work either.

Any ideas how I could do that?

Aucun commentaire:

Enregistrer un commentaire