I have a problem with this code in C++:
void f();
void g()
{
f();
}
f() is not defined anywhere in the project and g() is not called anywhere. I use the following cmake directives:
add_compile_options(-fdata-sections -ffunction-sections)
add_link_options(-Wl,--gc-sections)
Using mingw32 the linker complains that f() is missing, but that's not what I expect since g() should not be linked in the first place. Using gcc on linux the project links correctly. I suspect that mingw 1st creates an intermediate library. This is the verbose output
C:\msys64\mingw32\bin\g++.exe -g -Wl,--gc-sections -Wl,--whole-archive CMakeFiles\main.dir/objects.a -Wl,--no-whole-archive -o main.exe -Wl,--out-implib,libmain.dll.a -Wl,--major-image-version,0,--minor-image-version,0 @CMakeFiles\main.dir\linklibs.rsp
CMakeFiles\main.dir/objects.a(main.cpp.obj): In function `Z1gv':
C:/Users/Documents/Sandbox/testcmake/main.cpp:20: undefined reference to `f()'
collect2.exe: error: ld returned 1 exit status
--- EDIT ---
I can replicate the issue with this simple test:
main.cpp:
void f();
void g(){
f();
}
int main(){
return 0;
}
Then compile with:
gcc -o main main.cpp -fdata-sections -ffunction-sections -Wl,--gc-sections
In mingw32: undefined reference to `f()'
In linux: OK