2011-06-03 92 views
0

在不同的文件夾.o文件的可執行文件我有兩個C++文件在不同的位置問題創建使用克++

  1. hello.cc在/home/testing/src/impl/hello.cc
  2. 在/home/testing/src/msg/messages.cc messages.cc

在hello.cc我們有一個包括用於messages.h

所以我sucessfull使用以下命令編譯hello.cc 克++ -c -I /家/檢測/ SRC/MSG hello.cc(此生成的hello.o文件在/ home /測試/ SRC/IMPL /)

現在我編譯使用命令 的messages.cc G ++ -c messages.cc(此產生的在/ home /測試/ src目錄/味精/一個messages.o文件)

現在我想的是利用製作和可執行文件使用hello.o

以下命令

g ++ -o你好hello.o

這是我收到

 
Undefined      first referenced 
symbol        in file 
message        hello.o 
ld: fatal: Symbol referencing errors. No output written to hello 
collect2: ld returned 1 exit status 

請幫我解決這個問題

回答

4

您需要加載所有目標文件錯誤:

g++ -o hello hello.o path/to/messages.o 

由於連接基不具有自動的方式知道在哪裏可以找到符號。

+0

說如果我在不同的位置有這麼多的對象,那麼如何解決這個問題 – Tapsi 2011-06-03 11:27:25

+2

@Tapsi使用makefile,或通過將多個目標文件捆綁到靜態庫,或兩者兼而有之。 – 2011-06-03 11:30:50

1

的命令應該是,

g++ hello.o message.o -o hello // give the proper paths 

爲了使主要的可執行文件,你必須包括所有與他們的正確路徑所需的目標文件。否則它將導致鏈接器錯誤