Error when linking executable

Hi, guys. I am new to cmake currently and I am rewriting the building file of my project from Makefile to cmake.

When linking the executable, cmake says there has “undefined reference”, but I am sure I have added the correct libraries. So I display the debug info, and found that the only difference between my original Makefile command and cmake-generated command is the position of the linking flags. (The flags in the original command are after the output file while the flags in cmake command are before the output file.)

After I manually change the position of the flags, there have no such errors. So I want to figure out why the position of flags influences the linking process and how to solve it from my cmake file.

Here’s the probably helpful info. Can someone help? Thanks a lot ~

cmake-generated error command

/usr/bin/c++ -m64 -O2 -Wl,--no-undefined -nostdlib -nodefaultlibs -nostartfiles -L/opt/alibaba/teesdk/intel/sgxsdk/lib64 -Wl,--whole-archive -lsgx_trts -Wl,--no-whole-archive -Wl,--start-group -lsgx_tstdc -lsgx_tcrypto -lsgx_tservice -Wl,--end-group -Wl,-Bstatic -Wl,-Bsymbolic -Wl,--no-undefined -Wl,-pie,-eenclave_entry -Wl,--export-dynamic -Wl,--defsym,__ImageBase=0 -Wl,--gc-sections -Wl,-z,relro,-z,now,-z,noexecstack -Wl,--version-script=/root/QuoteGenerationSample/Enclave/Enclave.lds CMakeFiles/enclave.so.dir/Enclave.cpp.o CMakeFiles/enclave.so.dir/Enclave_t.c.o -o enclave.so

error info

/usr/bin/ld: CMakeFiles/enclave.so.dir/Enclave_t.c.o: in function `sgx_enclave_create_report':
Enclave_t.c:(.text.sgx_enclave_create_report+0x3f): undefined reference to `memcpy_s'
/usr/bin/ld: Enclave_t.c:(.text.sgx_enclave_create_report+0xb7): undefined reference to `memcpy_s'
/usr/bin/ld: Enclave_t.c:(.text.sgx_enclave_create_report+0x109): undefined reference to `memcpy_verw_s'
/usr/bin/ld: Enclave_t.c:(.text.sgx_enclave_create_report+0x126): undefined reference to `memcpy_verw_s'
/usr/bin/ld: Enclave_t.c:(.text.sgx_enclave_create_report+0x1f1): undefined reference to `memcpy_verw_s'
/usr/bin/ld: Enclave_t.c:(.text.sgx_enclave_create_report+0x217): undefined reference to `memcpy_verw_s'
/usr/bin/ld: Enclave_t.c:(.text.sgx_enclave_create_report+0x263): undefined reference to `memcpy_verw_s'
/usr/bin/ld: CMakeFiles/enclave.so.dir/Enclave_t.c.o:Enclave_t.c:(.text.sgx_enclave_c

manually fixed command

/usr/bin/c++ CMakeFiles/enclave.so.dir/Enclave_t.c.o CMakeFiles/enclave.so.dir/Enclave.cpp.o -o enclave.so -m64 -O2 -Wl,--no-undefined -nostdlib -nodefaultlibs -nostartfiles -L/opt/alibaba/teesdk/intel/sgxsdk/lib64 -Wl,--whole-archive -lsgx_trts -Wl,--no-whole-archive -Wl,--start-group -lsgx_tstdc -lsgx_tcrypto -lsgx_tservice -Wl,--end-group -Wl,-Bstatic -Wl,-Bsymbolic -Wl,--no-undefined -Wl,-pie,-eenclave_entry -Wl,--export-dynamic -Wl,--defsym,__ImageBase=0 -Wl,--gc-sections -Wl,-z,relro,-z,now,-z,noexecstack -Wl,--version-script=/root/QuoteGenerationSample/Enclave/Enclave.lds