【问题标题】:g++ - linking stage (-L flag) not workingg++ - 链接阶段(-L 标志)不起作用
【发布时间】:2018-11-12 22:11:19
【问题描述】:

我是 C++ 新手,正在尝试找出我的错误在编译过程中的哪个位置。抱歉,如果这个问题不清楚,我不确定要提供什么信息。

参考信息:目录“mbedtls/lib/”包含两个.a(归档)文件,“libmbedtls_SGX_t.a”和“libmbedtls_SGX_u.a”。

我正在使用 Makefile,它按顺序运行以下命令。这些命令似乎运行无误:

cc -m64 -O2 -fPIC -Wno-attributes -IApp -I/opt/intel/sgxsdk/include -Imbedtls/include -DNDEBUG -UEDEBUG -UDEBUG -c App/Enclave_u.c -o App/Enclave_u.o
g++ -m64 -O2 -fPIC -Wno-attributes -IApp -I/opt/intel/sgxsdk/include -Imbedtls/include -DNDEBUG -UEDEBUG -UDEBUG -std=c++11 -c App/App.cpp -o App/App.o
g++ -m64 -O2 -fPIC -Wno-attributes -IApp -I/opt/intel/sgxsdk/include -Imbedtls/include -DNDEBUG -UEDEBUG -UDEBUG -std=c++11 -c App/sgx_utils/sgx_utils.cpp -o App/sgx_utils/sgx_utils.o

运行这些命令后,它会运行以下命令:

g++ App/Enclave_u.o App/App.o App/sgx_utils/sgx_utils.o -o app -m64 -O2 -L/opt/intel/sgxsdk/lib64 -lsgx_urts_sim -lpthread -Lmbedtls/lib/ -lsgx_uae_service_sim

但是,此命令会产生错误:

App/Enclave_u.o: In function `Enclave_ocall_print_string':
Enclave_u.c:(.text+0x9): undefined reference to `ocall_print_string'
App/Enclave_u.o: In function `Enclave_ocall_mbedtls_net_free':
Enclave_u.c:(.text+0x28): undefined reference to `ocall_mbedtls_net_free'
App/Enclave_u.o: In function `Enclave_ocall_mbedtls_net_recv_timeout':
Enclave_u.c:(.text+0x54): undefined reference to `ocall_mbedtls_net_recv_timeout'
App/Enclave_u.o: In function `Enclave_ocall_mbedtls_net_send':
Enclave_u.c:(.text+0x71): undefined reference to `ocall_mbedtls_net_send'
App/Enclave_u.o: In function `Enclave_ocall_mbedtls_net_recv':
Enclave_u.c:(.text+0x91): undefined reference to `ocall_mbedtls_net_recv'
App/Enclave_u.o: In function `Enclave_ocall_mbedtls_net_usleep':
Enclave_u.c:(.text+0xa8): undefined reference to `ocall_mbedtls_net_usleep'
App/Enclave_u.o: In function `Enclave_ocall_mbedtls_net_set_nonblock':
Enclave_u.c:(.text+0xc9): undefined reference to `ocall_mbedtls_net_set_nonblock'
App/Enclave_u.o: In function `Enclave_ocall_mbedtls_net_set_block':
Enclave_u.c:(.text+0xe9): undefined reference to `ocall_mbedtls_net_set_block'
App/Enclave_u.o: In function `Enclave_ocall_mbedtls_net_accept':
Enclave_u.c:(.text+0x119): undefined reference to `ocall_mbedtls_net_accept'
App/Enclave_u.o: In function `Enclave_ocall_mbedtls_net_bind':
Enclave_u.c:(.text+0x144): undefined reference to `ocall_mbedtls_net_bind'
App/Enclave_u.o: In function `Enclave_ocall_mbedtls_net_connect':
Enclave_u.c:(.text+0x164): undefined reference to `ocall_mbedtls_net_connect'
collect2: error: ld returned 1 exit status
Makefile:184: recipe for target 'app' failed

我不知道为什么会产生这个错误。我的最后一个命令包括标志-Lmbedtls/lib/,它应该通过将存档文件(特别是 libmbedtls_SGX_u.a)链接到可执行文件来解决这些未定义的引用。然而,显然这并没有发生。

我需要在编译过程的早期链接存档文件吗?即 - 而不是在最终的 g++ 命令中使用标志 -Lmbedtls/lib/,我应该在前面的两个 g++ 命令中都使用它吗?

更新:将标志 -Lmbedtls/lib/ 添加到之前的 2 个 g++ 命令(生成 App.o 和 sgx_utils.o 的命令)并没有改变任何内容。错误仍然存​​在。

【问题讨论】:

  • -L 告诉 g++ 在哪里寻找 .a 文件,而不是实际链接它们,您必须指定 -llibmbedtls_SGX_u
  • 一般来说,您应该在提供符号的文件之前提及引用符号的文件。许多(不是全部,但大多数)链接器将丢弃他们以前没有看到过引用的任何符号。
  • @JesperJuhl 你能解释一下你的陈述对我应该执行的命令的含义吗?
  • 这意味着每当你告诉你的工具链链接一些东西时,你必须按照顺序命名目标文件/库,首先是消费/使用符号的那些和那些提供之后的实现。
  • 我认为是-pthread(不是-lpthread)。

标签: c++ g++ static-libraries static-linking sgx


【解决方案1】:

正如评论中提到的,只用-L指定一个目录而不指定该目录中的库名称是没有用的。

所以你必须另外用-l指定库名。

所以而不是:

g++ App/Enclave_u.o App/App.o App/sgx_utils/sgx_utils.o -o app -m64 -O2 \
   -L/opt/intel/sgxsdk/lib64 -lsgx_urts_sim -lpthread \
   -Lmbedtls/lib/ \
   -lsgx_uae_service_sim

用途:

g++ App/Enclave_u.o App/App.o App/sgx_utils/sgx_utils.o -o app -m64 -O2 \
   -L/opt/intel/sgxsdk/lib64 -lsgx_urts_sim -lpthread \
   -Lmbedtls/lib/ -lmbedtls_SGX_t -lmbedtls_SGX_u \
   -lsgx_uae_service_sim

【讨论】:

  • 我运行了命令g++ App/Enclave_u.o App/App.o App/sgx_utils/sgx_utils.o -o app -m64 -O2 -L/opt/intel/sgxsdk/lib64 -lsgx_urts_sim -lpthread -Lmbedtls/lib/ -llibmbedtls_SGX_u -lsgx_uae_service_sim,因为我只想链接libmbedtls_SGX_u.a。然而,令人惊讶的是,我收到了错误:“找不到 -llibmbedtls_SGX_u”,尽管我非常确定“libmbedtls_SGX_u.a”在“mbedtls/lib”目录中。
  • 命令-l 很奇特。您必须指定不带前导 lib 的库名称。所以它只是-lmbedtls_SGX_u 而不是-llibmbedtls_SGX_u。在这方面,我最初的答案部分不正确,您可能很快就复制了它。
  • 哦,哎呀,我以为你忽略了 lib 是个错误,所以我把它留在了。
  • 我可以去哪里学习更多关于如何使用g++编译器,这样我可以避免以后犯这些错误?
  • 我不这么认为。它也是一个图书馆。如果您有 Linux、Unix 或 macOS 作为操作系统,请尝试ls -l /usr/lib/*pthread*
猜你喜欢
  • 2017-02-27
  • 2011-10-11
  • 2018-08-04
  • 1970-01-01
  • 1970-01-01
  • 2020-09-07
  • 1970-01-01
  • 1970-01-01
  • 2013-01-27
相关资源
最近更新 更多