【发布时间】:2015-06-24 08:03:07
【问题描述】:
所以我一直在使用 makefile 和 g++ 在 Linux 中开发一个项目,但现在我想让它在 Visual Studio 上的 Windows 中运行。所以我的项目有一个特殊的 cpp 和 .h 文件。只有 1. 我将 .h 配置为自动将该 .h 文件包含在所有 cpp 文件中,但特殊的 cpp 文件除外。但是,我似乎无法在 Visual Studio 中这样做。
我基本上是在编写自己的 vcxproj 文件,所以我查找了配置设置,并在设置中找到了 Force Include 标志。但我似乎无法找到解决这个例外的方法。
我还尝试修改 .h 文件,以便它可以识别它的包含位置并通过 #if 和“__FILE__”等预处理指令表现出不同的行为。但我发现“__FILE__”会以任何方式返回 .h,而不是源代码。
我在谷歌上搜索的想法和关键字用完了。那么有什么想法或线索吗?
提前致谢。
编辑:
在 Linux 上运行的示例代码:
特殊.h
class Test{
private:
Test() = delete;
Test(const Test&) = delete;
Test(Test&&) = delete;
public:
void print(const char*);
};
extern TEST t1;
Special.cpp
#include <iostream>
class Test{ //Singleton Class
private:
Test(); //Note that this line is different from the .h
Test(const Test&) = delete;
Test(Test&&) = delete;
public:
static Test& getInstance(); //Note that this line is missing from the .h
void print(const char*);
};
Test::Test(){}
Test& Test::getInstace(){
static Test inst;
return return inst;
}
void Test::print(const char* msg){
std::cout << msg << std::endl;
}
Test t1 = Test::getInstance();
Main.cpp
int main(){
t1.print("Hello World!");
}
生成文件:
all: App-Main
App-Main: Main.o Special.o
g++ Main.o Special.o
Special.o: Special.cpp
g++ -c $< $@ -std=c++11
%.o: %.cpp
g++ -c $< $@ -std=c++11 -include Special.h
这就是目前在 Linux 中运行的代码。没有构建错误什么都没有。
如果我们在Special.cpp 中包含Special.h,就会出现问题。
现在,请假设有理由不在 cpp 文件中包含 .h 文件。
【问题讨论】:
-
详细描述好,但不如代码。你能创建一个Minimal, Complete, and Verifiable Example 给我们看吗?如果您遇到构建错误,请您也显示它们吗?
-
刚刚添加了一个工作示例的示例。我的问题不是构建错误,而是设计问题。我正在寻找一种解决方案,既可以在 Visual Studio 中进行异常处理,也可以将其包含在
Special.cpp中的Special.h中,但它就像Special.h为空一样。如果有什么东西可以让编译器忘记之前声明的东西。像#undef 之类的东西。我不知道,我现在只是猜想。
标签: c++ visual-studio visual-c++ makefile