【发布时间】:2015-04-25 08:29:33
【问题描述】:
我认为我尝试的东西不够花哨,值得“插件”这个词,但我正在尝试做的事情:
给定文件 a.h、a.cpp 和 main.cpp,我想创建其他文件,例如:
g++ -o test main.cpp a.cpp b.cpp
导致测试程序做某事,并且
g++ -o test main.cpp a.cpp c.cpp
做别的事。
这部分我已经开始工作了,参见下面的代码。 我的问题是:有没有可能
g++ -o test main.cpp a.cpp
做一些默认行为?我尝试了几件事,但我总是得到一些未定义的东西。
到目前为止我的代码:
// a.h
#ifndef A_H_
#define A_H_
class A {
public:
A();
~A();
virtual void print()=0;
};
#endif
// a.cpp
#include <iostream>
#include "a.h"
A::A(){}
A::~A(){}
// b.h
#include "a.h"
class B: public A {
public:
B();
~B();
void print();
};
// b.cpp
#include <iostream>
#include "b.h"
B::B(){}
B::~B(){}
void B::print(){
std::cout << "I am B" << std::endl;
}
A* a = new B();
// code in c.h and c.cpp similar to the one in b.h and b.cpp
// just "B" replaced by "C"
// main.cpp
#include "a.h"
extern A* a;
int main(){
a->print();
}
针对 b.cpp 编译时,代码打印“I am B”,针对 c.cpp 编译时,代码打印“I am C”。
我想要:
g++ -o test main.cpp a.cpp
让测试要么什么都不做,要么做一个默认行为。不需要很简单。
【问题讨论】: