Re: [問題] compiler head file與多個檔在linuxm卮cc
※ 引述《imio24 (imio)》之銘言:
: :開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
: 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: 111.241.136.160
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 2 之 3 篇):
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章