【问题标题】:How can I link an .o file using g++如何使用 g++ 链接 .o 文件
【发布时间】:2010-02-04 22:05:55
【问题描述】:

我正在尝试使用 g++ 编译一个 .cc 文件,我需要它来链接一个 .o 文件。

所以我尝试了:

$g++ -o client -I../ipc -L../messages.o client.cc
/usr/bin/ld: error: ../messages.o: can not read directory: Not a directory

我已经试过了:

$g++ -o client -I../ipc -l../messages.o client.cc
/usr/bin/ld: error: cannot find -l../messages.pb.o
$$ ls -l ../messages.o

-rw-r--r-- 1 hap497 hap497 227936 2010-02-03 22:32 ../messages.o

您能告诉我如何在 .o 文件中进行链接吗?

谢谢。

【问题讨论】:

    标签: c++ g++


    【解决方案1】:
    $g++ -o client -I../ipc client.cc ../messages.o
    

    【讨论】:

    • 应该这样做。 OP的问题是 -L 指定了搜索库的路径,而 -l 指定了要在该路径中查找的库。目标文件只需作为参数给出。
    • 谢谢。我试过'$g++ -o client -I../ipc client.cc ../messages.o' 但结果messages.o需要libprotobuf.a库。所以我尝试了 'g++ -o client -I../ipc client.cc -l/usr/local/lib/libprotobuf.a -lpthread ../messages.o;我仍然得到'/usr/bin/ld: ../messages.o: in function ipc::protobuf_AssignDesc_messages_2eproto():ipc/messages.pb.cc:33: error: undefined reference to 'google::protobuf:: DescriptorPool::generated_pool()' 感谢您提供更多想法?
    • 代替-l/usr/local/lib/libprotobuf.a,试试-lprotobuf/usr/local/lib 可能在您的默认路径中;如果没有,请添加-L/usr/local/lib-l 选项将“lib”添加到前面,将“.a”添加到末尾。
    • 本可以在你的回答中使用一些解释,但没关系,@FredLarson 做得很好。
    猜你喜欢
    • 2013-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-25
    • 2020-09-17
    相关资源
    最近更新 更多