我正在尝试编译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
。最初,Internet似乎表明Mac编译器有时无法使用成员初始化列表来编译static const map
(如果我删除了static const
部分,它也会抱怨)。但是,当我注释掉与std::map
有关的所有内容(如上所述保留makefile
)或将所有文件放在同一文件夹中时(将makefile
和#include
都重写到test.cpp
中) ]),一切正常,但我想同时使用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
}
}
}
}
作为@ S.M。指出,您必须更改my_stuff.o
规则,但必须更改配方以及目标,以便它实际上可以构建您想要的东西:
../OtherFiles/my_stuff.o: ../OtherFiles/my_stuff.cpp ../OtherFiles/my_stuff.hpp
$(CC) $(CFLAGS) -c $< -o $@
更笼统地说,在尝试操纵语言之前,您必须先了解该语言。换入和换出补丁以查看有效的方法,这是编写代码的一种非常低效的方式。