我试图弄清楚如何使用GCC为#include语句构建变量字符串.
我的想法是,对于我编写的每个源模块,我希望包含一个动态生成的C源头,它是在构建过程的早期创建的.
生成此文件不是问题.不幸的是,包括它在内.
到目前为止我所拥有的是(identities.h):
// identities.h
# define PASTER2(str) #str
# define PASTER(str) PASTER2(str ## .iden)
# define EVALUATOR(x) PASTER(x)
# define IDENTITIES_FILE EVALUATOR(__FILE__)
# include IDENTITIES_FILE
Run Code Online (Sandbox Code Playgroud)
理想情况下,这将被使用(main.c):
//main.c
# include "identities.h"
int main() {return 0;}
Run Code Online (Sandbox Code Playgroud)
在编译之前,预处理器将在一次传递中扩展为:
//main.c (preprocessed)
# include "main.c.iden"
int main() {return 0;}
Run Code Online (Sandbox Code Playgroud)
我正在使用的两个间接级别(PASTER和EVALUATOR)是这篇文章的结果.
不幸的是,这不起作用,我留下了错误:
obj/win32/dbg/main.o
In file included from main.c:1:0:
identities.h:42:1: error: #include expects "FILENAME" or <FILENAME>
Run Code Online (Sandbox Code Playgroud)
我认为问题是include语句缺少引号..任何想法?
我正在编写一个C++应用程序,我想让开发人员在编译时选择用于特定问题的算法.这两种算法都是作为实现通用接口的C++类实现的,并且是相互替代的替代品.它们都有.h和.cpp文件,并且驻留在子目录中(让我们称之为impl/).
在我的Makefile中,我有类似的东西:
...
IMPL = default
...
binary: ... impl/$(IMPL).o
...
impl/%.o: impl/%.cpp impl-interface.h impl/%.h
...
%o: %.cpp ...
$(CXX) $(CXXFLAGS) -DIMPL=$(IMPL) -c -o $@ $*.cpp
Run Code Online (Sandbox Code Playgroud)
这个想法是用户应该能够输入make binary IMPL=thatimpl.
在任何想要使用用户选择的算法的文件中,我会这样做:
IImpl o = new IMPL();
Run Code Online (Sandbox Code Playgroud)
但是,这要求我包含所选实现的头文件.不幸的是,C++需要#include后跟a "string",a <libfile>.您也可以使用此处建议的宏,但它要求宏的参数为文字字符串.如果我使用:
#define QUOTEME(M) #M
#define INCLUDE_FILE(M) QUOTEME(impl/##M##.h)
#include INCLUDE_FILE(IMPL)
Run Code Online (Sandbox Code Playgroud)
编译器将尝试包含文字字符串impl/IMPL.h,而不是扩展IMPL到传递给make编译器的任何内容.
关于如何实现这一点的任何指示都将非常受欢迎!
我在文件中有一堆#include语句:
#include "/Users/cooper/Desktop/MyLib/graph_api.h"
#include "/Users/cooper/Desktop/MyLib/mst.h"
#include "/Users/cooper/Desktop/MyLib/dfs.h"
#include "/Users/cooper/Desktop/MyLib/bfs.h"
#include "/Users/cooper/Desktop/MyLib/topo_sort.h"
#include "/Users/cooper/Desktop/MyLib/scc.h"
#include "/Users/cooper/Desktop/MyLib/bipartite.h"
#include "/Users/cooper/Desktop/MyLib/dijkstra.h"
#include "/Users/cooper/Desktop/MyLib/union_find.h"
#include "/Users/cooper/Desktop/MyLib/my_string.h"
#include "/Users/cooper/Desktop/MyLib/2d_array.h"
Run Code Online (Sandbox Code Playgroud)
但是,它可能会在未来发生变化,我将不得不更新许多行.有没有可能有类似的东西
PATH = "/Users/cooper/Desktop/MyLib/
#include PATH + "2d_array.h"
Run Code Online (Sandbox Code Playgroud)
?