繁体   English   中英

包含来自不同文件夹的文件的 makefile(对于 C++)

[英]makefile with files from different folders (for C++)

我正在尝试编译一个 C++ 测试文件,该文件应该从项目文件结构中相邻文件夹中的文件进行编译。 我有以下几点:

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

对于编译,我试图离开my_stuff.o在文件OtherFiles文件夹,所以如果我有其他makefiles ,他们没有重新编译每个单独的版本。

我的makefile如下所示:

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 $<

我想了一会儿这个设置有效,但后来我开始遇到一些奇怪的问题并且无法编译。 例如,有一个static const map产生error: expected ';' after top level declarator error: expected ';' after top level declarator 起初,互联网似乎表明 Mac 编译器有时无法编译带有成员初始化列表的static const map s(如果我删除了static const部分,它也会抱怨)。 但是,当我注释掉与std::map (如上所述保留makefile )或将所有文件放在同一文件夹中(重写makefile以及test.cpp#include s)时,所有内容没问题,但我想同时使用std::map和所选的文件结构。 哦,删除额外的警告标志也不起作用。

任何想法我怎么能做到这一点?

编辑

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
  }
}

测试部分是一个catchMain.cpp

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

和实际测试, my_tests.cpp

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

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

正如@SM 指出的那样,您必须更改my_stuff.o规则,但您必须更改配方和目标,以便它真正构建您想要的东西:

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

更一般地说,您必须先了解该语言,然后才能尝试操作它。 交换补丁进出以查看哪些有效是编写代码的一种非常低效的方式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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