[英]When seperating class file into .h and .cpp file how can I define it in the main file by including the .h file?
[英]How do I get a function/class definition from a .cpp by including its corresponding .h file?
例如,在Ah中:
class A{
void method();
};
在A.cpp中:
#include "A.h"
void A::method(){/*do stuff*/};
和在main.cpp中
#include "A.h"
int main(){
A a;
a.method();
}
如何通過在main.cpp文件中僅包含Ah來從A.cpp訪問方法的定義? 是否有涉及Makefile或IDE的技巧?
是否有涉及Makefile或IDE的技巧?
這種“技巧”稱為鏈接-將所有已編譯的模塊和庫一起加載到可執行文件中。 您可以手動進行操作:
g++ -c a.cpp -o a.o // compiling a.cpp and producing object file a.o
g++ -c main.cpp -o main.o // compiling main.cpp and producing object file main.o
g++ main.o a.o -o myprog // linking all object files together with system libraries and producing executable myprog
對於不同的編譯器,命令看起來可能有所不同,但是過程相同。 當然,您不想一次又一次地輸入所有內容,因此您希望將其自動化。 IDE或makefile為您完成的工作不涉及任何技巧或魔術。
我通過向項目中添加一個新的.cpp文件來解決這個問題,該文件由Xcode自動鏈接到相應的.hpp文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.