Search code examples
c++g++static-librariesstatic-linkingsgx

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


I am new to C++ and am trying to figure out where in the compiling process my error is. Apologies, if this question is unclear, I'm not sure what information to provide.

Reference information: The directory "mbedtls/lib/" contains two .a (archive) files, "libmbedtls_SGX_t.a" and "libmbedtls_SGX_u.a".

I am using a Makefile, which runs the following commands in order. The commands seem to run error free:

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

After running those commands, it runs this command:

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

However, this command produces an error:

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

I don't know why this error is being produced. My last command includes the flag -Lmbedtls/lib/ which should resolve these undefined references by linking the archive files (specifically - libmbedtls_SGX_u.a) to the executable. However, clearly this is not happening.

Do I need to link the archive files earlier in the compile process? I.e - instead of using the flag -Lmbedtls/lib/ in the final g++ command, should I be using it in both of the previous g++ commands?

Update: Adding the flag -Lmbedtls/lib/ to the previous 2 g++ commands (the ones that generated App.o and sgx_utils.o) did not change anything. The error remains.


Solution

  • As has been mentioned in the comment, only specifying a directory with -L without specifying the name of a library in that directory is useless.

    So you have to additionally specify the library names with -l.

    So instead of:

    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
    

    Use:

    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