简体   繁体   中英

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 catchMain.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 error: expected ';' after top level declarator . At first, Internet seemed to indicate that Mac compilers sometimes aren't able to compile static const map s 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 #include s in test.cpp ), everything is ok, but I'd like to use both std::map s AND the chosen file structure. Oh, and removing the extra warning flags doesn't work either.

Any ideas how I could do that?

Edit

my_stuff.hpp :

namespace my_stuff {
   void function();
}

my_stuff.cpp :

#include "my_stuff.hpp"
#include <map>

namespace my_stuff {
  static const std::map<char, char> the_map {{'a', 'b'}, {'c', 'd'}};
  void my_function() {
    // map stuff
  }
}

The test part is both a vanilla catchMain.cpp :

#define CATCH_CONFIG_MAIN
#include "../../Catch2/catch.hpp" //which is outside the project specifics

and the actual tests, my_tests.cpp :

#include "../../Catch2/catch.hpp"
#include "../OtherFiles/my_stuff.hpp"
#include <map>

SCENARIO("", "") {
  GIVEN("") {
    WHEN("") {
      THEN("") {
        my_function();
        // Other stuff
      }
    }
  }
}

As @SM pointed out, you must change the my_stuff.o rule, but you must change the recipe as well as the target, so that it will actually build the thing you want:

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

More generally, you must understand the language before you attempt to manipulate it. Swapping patches in and out to see what works is a very inefficient way to write code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM