[問題] Compile的問題

看板C_and_CPP (C/C++)作者 (實況「野球」魂)時間15年前 (2011/07/14 17:20), 編輯推噓0(002)
留言2則, 2人參與, 最新討論串1/2 (看更多)
我現在用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

07/14 17:25, , 1F
MyLib.cpp include MyLib.h之前define __INCLUDE_FUN2__
07/14 17:25, 1F

07/14 17:28, , 2F
MyApp1 Compile時不要加__INCLUDE_FUN2__ MACRO
07/14 17:28, 2F
文章代碼(AID): #1E7hJ10J (C_and_CPP)
討論串 (同標題文章)
以下文章回應了本文
完整討論串 (本文為第 1 之 2 篇):
0
2
文章代碼(AID): #1E7hJ10J (C_and_CPP)