Re: [問題] compiler head file與多個檔在linuxm卮cc

看板C_and_CPP (C/C++)作者 (imio)時間13年前 (2012/10/07 04:00), 編輯推噓2(209)
留言11則, 7人參與, 最新討論串3/3 (看更多)
感謝!! 我compiler 成功了. 我是先compiler (g++ -g helloWorld.h -o helloWorld)我的head file helloWorld.h 去生成一個helloWorld 輸出檔. 再用 g++ -g -c hellowWorld.cpp -o helloworld 生成hellworld輸出檔並不做連結 最後 g++ -g main.cpp -o main helloworld 生成main 可執行檔並與helloworld連結. 如果我的認知有錯請指教 謝謝. ※ 引述《cuteclare (清兒)》之銘言: : ※ 引述《imio24 (imio)》之銘言: : : Linux : : 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) : : 問題(Question): : : 請問一下, 我想用G++ 在linux 下compiler head file 和 : : cpp file (如g++ -g main.cpp -o main) 當我compiler hellowWorld.h 沒有問題. : : 如果我在compiler hellowWorld.cpp 錯誤為 : : In function `_start':(.text+0x20): undefined reference to `main' collect2: ld returned 1 exit status. : 在這邊假設原po所下的指令是 : g++ -g helloWorld.cpp -o helloWorld.o : 請改成 g++ -g -c helloWorld.cpp -o helloWorld.o : 不然編譯器會認為這應該是一個執行檔 所以應該有 int main(...) : 之類的function 存在於這 helloWorld.cpp 之中 : : 如果compiler main.cpp 錯誤為 undefined reference to `HelloWorld::printGreeting()'collect2: ld returned 1 exit status : 假設原po下達的指令是g++ -g main.cpp -o main.o : 兩個問題 : 1.應該是貼到舊的code之類吧 如果是按照這 3個code來看 : 得到的錯誤訊息應該是undefined reference to `helloWorld()'collect2: ld returned 1 exit status : 我"猜"會造成 undefined reference to 'HelloWorld::printGreetin()' .... : 有可能是用到了namespace : hellowWorld.h 應該類似如下 : namespace HelloWorld { : void printGreeting(); : } : 當然在main.cpp 會是引用到 HelloWorld::printGreeting(); : 可能要去檢視一下 會不會是compile到其他檔案 or 有舊的 .o殘存的可能 : 2. 應該是要把 main.cpp 所需要的helloWorld.o add 給予compile知悉 : 請嘗試以下指令 : g++ -g main.cpp -o main helloWorld.o : : 請問我要如何compiler才能把他們連起來? : : 餵入的資料(Input): : : 預期的正確結果(Expected Output): : : 錯誤結果(Wrong Output): : : 程式碼(Code):(請善用置底文網頁, 記得排版) : : 1 helloWorld.h: : : 2 #ifndef HELLOWORLD : : 3 #define HELLOWORLD : : 4 : : 5 void helloWorld(); : : 6 : : 7 #endif : : 8 : : 9 : : 10 helloWorld.cpp : : 11 #include "helloWorld.h" : : 12 #include <iostream> : : 13 : : 14 void helloWorld() : : 15 { : : 16 std::cout << "Hello World!" << std::endl; : : 17 } : : 18 : : 19 : : 20 main.cpp: : : 21 #include "helloWorld.h" : : 22 : : 23 int main() : : 24 { : : 25 helloWorld(); : : 26 return 0; : : 27 } : : 補充說明(Supplement): -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 71.190.195.62

10/07 09:12, , 1F
只對了一半不到,算矇出來的。
10/07 09:12, 1F

10/07 11:18, , 2F
那請問正確是?
10/07 11:18, 2F

10/07 12:12, , 3F
連 compiler 是名詞還是動詞都搞不清楚
10/07 12:12, 3F

10/07 12:36, , 4F
英文差一點沒關係啦~只要是溝通上沒有障礙就好
10/07 12:36, 4F

10/07 12:38, , 5F
反正又不是在做...核能發電廠之類的(?
10/07 12:38, 5F

10/07 12:40, , 6F
樓上XDD
10/07 12:40, 6F

10/07 13:14, , 7F
小地方還是注重一點的好,觀感問題
10/07 13:14, 7F

10/07 13:54, , 8F
是我compile沒注意. 不過請問他們正確觀系是如何
10/07 13:54, 8F

10/07 15:39, , 9F
compile=>verb compiler=>noun "header file"
10/07 15:39, 9F

10/07 18:00, , 10F
編譯=>動詞編譯器=>名詞「標頭檔」 XDD
10/07 18:00, 10F

10/07 19:30, , 11F
第一篇就有人回你啊,header 檔不需要拿去 compile
10/07 19:30, 11F
文章代碼(AID): #1GS8tetM (C_and_CPP)
文章代碼(AID): #1GS8tetM (C_and_CPP)