使用不同文件夹中的文件生成文件(对于C+;+;) 我试图编译一个C++测试文件,它应该从项目文件结构中的相邻文件夹中的文件编译。我有以下资料: Project/TestFiles/makefile Project/TestFiles/test.cpp Project/OtherFiles/my_stuff.cpp Project/OtherFiles/my_stuff.hpp
对于编译,我试图将使用不同文件夹中的文件生成文件(对于C+;+;) 我试图编译一个C++测试文件,它应该从项目文件结构中的相邻文件夹中的文件编译。我有以下资料: Project/TestFiles/makefile Project/TestFiles/test.cpp Project/OtherFiles/my_stuff.cpp Project/OtherFiles/my_stuff.hpp,c++,c++11,makefile,g++,C++,C++11,Makefile,G++,对于编译,我试图将my_stuff.o文件保留在OtherFiles文件夹中,因此如果我有其他makefile,它们不必各自重新编译单独的版本 我的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)
my_stuff.o
文件保留在OtherFiles
文件夹中,因此如果我有其他makefile
,它们不必各自重新编译单独的版本
我的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 $<
my_stuff.cpp
:
namespace my_stuff {
void function();
}
#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
}
}
#define CATCH_CONFIG_MAIN
#include "../../Catch2/catch.hpp" //which is outside the project specifics
#include "../../Catch2/catch.hpp"
#include "../OtherFiles/my_stuff.hpp"
#include <map>
SCENARIO("", "") {
GIVEN("") {
WHEN("") {
THEN("") {
my_function();
// Other stuff
}
}
}
}
和实际测试,my_tests.cpp
:
namespace my_stuff {
void function();
}
#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
}
}
#define CATCH_CONFIG_MAIN
#include "../../Catch2/catch.hpp" //which is outside the project specifics
#include "../../Catch2/catch.hpp"
#include "../OtherFiles/my_stuff.hpp"
#include <map>
SCENARIO("", "") {
GIVEN("") {
WHEN("") {
THEN("") {
my_function();
// Other stuff
}
}
}
}
#包括“../../Catch2/catch.hpp”
#包括“./OtherFiles/my_stuff.hpp”
#包括
场景(“,”){
给定(“”){
何时(“”){
然后(“”){
my_函数();
//其他东西
}
}
}
}
正如@S.M.所指出的,您必须更改my_stuff.o
规则,但您必须更改配方和目标,以便它能够真正构建您想要的东西:
../OtherFiles/my_stuff.o: ../OtherFiles/my_stuff.cpp ../OtherFiles/my_stuff.hpp
$(CC) $(CFLAGS) -c $< -o $@
。/OtherFiles/my_stuff.o:../OtherFiles/my_stuff.cpp../OtherFiles/my_stuff.hpp
$(CC)$(CFLAGS)-c$<-o$@
更一般地说,在尝试操作语言之前,您必须理解该语言。在编写代码时,交换补丁以查看其效果是一种非常低效的方法。目标
测试.out
将永远找不到所需的。/OtherFiles/my_stuff.o
。目标my_stuff.o:
必须是。/OtherFiles/my_stuff.o
。有什么办法吗?显示代码和完整的错误消息。子目录中是否也有makefiles?如果是这样,您也应该从最上面的makefile中使用它们<代码>make-C subdir[target]您必须使用make吗?您可能会发现cmake easierThere,添加了一些代码。虽然我并不自称是专家,但我认为我对它的理解足以满足我的用例。我也不是随意交换零碎的东西,而是解决问题。我很困惑,为什么当我添加某些代码或将所有代码放在同一个位置时,它似乎会崩溃。不幸的是,您建议的解决方案不起作用,并给出了clang:error:没有这样的文件或目录:'../OtherFiles/my_stuff.o'
。还有其他想法吗?@GreatName:它还在做什么?它还有其他信息吗?它是否构建“../OtherFiles/my_stuff.o”?它似乎在不同的情况下使用了不同的编译器来处理makefile
。当我将文件拆分为单独的相邻文件夹时,它使用(我猜)一个旧的,没有最新功能的文件夹。如果这是准确的,我如何强制make比我已经拥有的更明确地使用某个编译器或版本?我使用Mac终端,G++--version
:Apple LLVM版本9.1.0(clang-902.0.39.2);目标:x86_64-apple-darwin17.7.0;线程型号:posix