作者mida (實況「野球」魂)
看板C_and_CPP
標題[問題] Compile的問題
時間Thu Jul 14 17:19:58 2011
我現在用VS建了三個project: MyApp1, MyApp2, MyLib
MyLib是一個static library,
內容大概是
MyLib.h
-----------------------------
#ifndef __MY_LIB_H__
#define __MY_LIB_H__
class MyLib
{
public:
void Function1();
#ifdef __INCLUDE_FUN2__
void Function2();
#endif
};
#endif
------------------------------
MyLib.cpp
------------------------------
#ifndef __MY_LIB_H__
#include "MyLib.h"
#endif
//
//
//
void MyLib::Function1()
{
}
#ifdef __INCLUDE_FUN2__
//
//
//
void MyLib::Function2()
{
}
#endif
------------------------------
這個static library可以compile成功沒有問題。
MyApp1和MyApp2是.exe
MyApp1
-----------------------------------------
include "MyApp.h"
#pragma comment(lib, "MyLib.lib")
MyLib Lib;
int main()
{
Lib.Function1();
}
-----------------------------------------
MyApp2
-----------------------------------------
#define __INCLUDE_FUN2__
include "MyApp.h"
#pragma comment(lib, "MyLib.lib")
MyLib Lib;
int main()
{
Lib.Function1();
Lib.Function2();
}
-----------------------------------------
以上MyApp1.exe可以正確產生,
但是MyApp2.exe會出現error LNK2001: unresolved external symbol
後來想想,因為MyLib.cpp在compile時,並沒有define __INCLUDE_FUN2__
才會有這個錯誤。
但是我想要的是MyApp1.exe compile的時候,不要去include Function2,
所以我應該要怎樣修正,可以讓這2個程式可以共用MyLib.lib,
而MyApp1.exe看不到也不會include Function2,
但MyApp2.exe則可以呢?
而且前題是MyLib.lib, MyApp1.exe, MyApp2.exe修改好後,不用在額外改code,
各compile一次就可以達到這樣的目標呢?
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 220.135.250.104
→ PkmX:MyLib.cpp include MyLib.h之前define __INCLUDE_FUN2__ 07/14 17:25
→ karcher:MyApp1 Compile時不要加__INCLUDE_FUN2__ MACRO 07/14 17:28