Search code examples
c++cmakelinker-errorsintel-oneapi

Link options are inserted before /link keyword in CMakeLists (Intel OneAPI compiler)


I'm getting a linker error because of a conflict between liburctd.lib and libmmt.lib. Why I'm getting that is a whole other question.

In trying to figure that out, I wanted to see if just setting /NODEFAULTLIB (or -nodefaultlib) worked.

I've tried three different ways of adding it (with three different libraries to be able to distinguish the errors, slashes/dashes/uppercase/quotes all don't make a difference):

set_target_properties(SA PROPERTIES LINK_FLAGS -nodefaultlib:libcmt.lib)
target_link_options(SA PUBLIC -nodefaultlib:libmmt.lib)
target_link_libraries(SA PUBLIC -nodefaultlib:liburctd.lib)

Running with verbose commands shows me:

LINK: command "C:\PROGRA~2\Intel\oneAPI\compiler\latest\bin\icx.exe /nologo @CMakeFiles\SA.rsp /Qoption,link,/machine:X86 /Qoption,link,/debug /Qoption,link,/INCREMENTAL /Qoption,link,/subsystem:console -nodefaultlib:libcmt.lib -nodefaultlib:libmmt.lib /link /out:SA.exe /implib:SA.lib /pdb:SA.pdb /version:0.0 /MANIFEST:EMBED,ID=1" failed (exit code 1169) with the following output:
icx: warning: unknown argument ignored in clang-cl: '-nodefaultlib:liburctd.lib' [-Wunknown-argument]
icx: warning: unknown argument ignored in clang-cl: '-nodefaultlib:libcmt.lib' [-Wunknown-argument]
icx: warning: unknown argument ignored in clang-cl: '-nodefaultlib:libmmt.lib' [-Wunknown-argument]
libucrtd.lib(log_impl.obj) : error LNK2005: __CIlog already defined in libmmt.lib(log_iface_c99.obj)
libucrtd.lib(pow_impl.obj) : error LNK2005: __CIpow already defined in libmmt.lib(pow_iface_c99.obj)

Focusing in on the important part of the command:

... -nodefaultlib:libcmt.lib -nodefaultlib:libmmt.lib /link /out:SA.exe  ...

I read somewhere that -nodefaultlib is a flag that needs to come after /link

And indeed, running the command manually with -nodefaultlib:libmmt.lib after /link makes my program link just fine!

What is happening, why is it happening, what am I doing wrong?

I've been looking for the better part of 4 hours yesterday and I can't for the life of me find anything at all about this.


(If someone has a solution for my linker error, which oddly enough occurs in my 32 bit debug build but not in my 64 bit one, that's also much appreciated)


Solution

  • It should be configured like below, as you have shown other linker options in the question.

    target_link_options(SA PUBLIC /Qoption,link,/nodefaultlib:libmmt.lib)
    

    AFAIK target_link_libraries as shown is not needed, otherwise the option is duplicated.