简体   繁体   中英

unresolved external symbol referenced in function removed by /OPT:REF

I have a function bar() that I don't actually call, that calls unimplemented foo():

void foo();
void bar()
{
    foo();
}
int main()
{

}

If I package each function in a separate section/COMDAT and ask the linker to remove unused sections/COMDAT, I can get gcc to compile the program

gcc -ffunction-sections -Xlinker --gc-sections LinkerFunctions.cpp

but the equivalent in Visual C++ 2019

cl /Gy LinkerFunctions.cpp /link /OPT:REF

barks that

error LNK2019: unresolved external symbol "void __cdecl foo(void)" (?foo@@YAXXZ) referenced in function "void __cdecl bar(void)" (?bar@@YAXXZ)

How can I get msvc to compile the program?

Got your example working by adding inline , even after adding __declspec(noinline) to prevent actual inlining.

void foo();

__declspec(noinline)
inline void bar()
{
    foo();
}

int main()
{

}

Tried because the documentation says:

Inlined functions and member functions defined inside a class declaration are always COMDATs.

Not sure however if it is robust solution, or works just in this particular case.

Accidentally found a linker switch for that:

cl incomplete.cpp /link /FORCE:UNRESOLVED

Will give a warning but in fact will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM