2011-06-03 96 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 

为了使主要的可执行文件,你必须包括所有与他们的正确路径所需的目标文件。否则它将导致链接器错误