g++ - 链接阶段(-L 标志)不工作

g++ - linking stage (-L flag) not working

我是 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/,它应该通过 link 将归档文件(特别是 - libmbedtls_SGX_u.a)解析为可执行文件来解决这些未定义的引用。然而,显然这不会发生。

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

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

正如评论中提到的,只指定目录 -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