1

I am in the middle of porting data from a Visual Studio project to Mingw GCC. I believe I have run into a library linking order issue and am not really sure on how to circumvent this issue. I currently have two files CWavImaData.cpp and CWavData.cpp. Now both these files contain the following statement

{    
WAVEFORMATEXTENSIBLE *pex = reinterpret_cast<WAVEFORMATEXTENSIBLE*>(new char[sizeof(WAVEFORMATEXTENSIBLE)]);
    ....
    ....
    pex->SubFormat = KSDATAFORMAT_SUBTYPE_PCM; <-- Conflicting Statement
}

Now KSDATAFORMAT_SUBTYPE_PCM depeds on the library libksguid.a. Now if I comment out the conflicting statement in CWavData.cpp the project builds fine. However if I un-comment this conflicting statement I get the linker error.

CWavData.cpp:156: undefined reference to `_GUID const& __mingw_uuidof<KSDATAFORMAT_SUBTYPE_PCM_STRUCT>()'

This makes me think that there is a linker error. Here is my build output

g++.exe -shared -Wl,--output-def=libsndlib.def -Wl,--out-implib=libsndlib.a -Wl,--dll -LC:\mingw64\x86_64-w64-mingw32\lib -Lc:\MyProj\SharedFunctions\ -L"Win32\Debug x64" "Win32\Debug x64\ADPCM\CWavImaData.o" "Win32\Debug x64\CCategoryLst.o" "Win32\Debug x64\CSampleList.o" "Win32\Debug x64\CSeqScptList.o" "Win32\Debug x64\CSndDrv.o" "Win32\Debug x64\CSndLib.o" "Win32\Debug x64\CSndRam.o" "Win32\Debug x64\CSndScptList.o" "Win32\Debug x64\CSndSeq.o" "Win32\Debug x64\CSndStrHdl.o" "Win32\Debug x64\CWavData.o" "Win32\Debug x64\CWavDataList.o" "Win32\Debug x64\CWavDataStr.o" "Win32\Debug x64\dllmain.o" "Win32\Debug x64\misc\assert\myassert.o" "Win32\Debug x64\misc\CFindFile.o" "Win32\Debug x64\misc\CStrings.o" "Win32\Debug x64\misc\mydxerr.o" "Win32\Debug x64\SndInt.o"  -o sndlib.dll  -lksguid -lSharedFunctions -lole32 -lwinmm -ldsound -lksguid -lksguid
Win32\Debug x64\CWavData.o: In function `CWavData::AnalyzeFormatChunk(_iobuf*, long)':
C:/MyProj/sndlib/CWavData.cpp:156: undefined reference to `_GUID const& __mingw_uuidof<KSDATAFORMAT_SUBTYPE_PCM_STRUCT>()'
collect2.exe: error: ld returned 1 exit status

I even tried adding ksguid twice however I am still getting a linker error. The file CWavImaData.cpp builds fine alone with this statement however when this statement is included with the file CWavData.cpp I get a linker error. Any suggestions on how I can resolve this issue ?

Update: I have been suggested to use --start-group. After trying it this is what my output looks like

g++.exe -shared -Wl,--output-def=libsndlib.def -Wl,--out-implib=libsndlib.a -Wl,--dll -LC:\mingw64\x86_64-w64-mingw32\lib -Lc:\MyProj\SharedFunctions\SharedFunctions\ -L"Win32\Debug x64" "Win32\Debug x64\ADPCM\CWavImaData.o" "Win32\Debug x64\CCategoryLst.o" "Win32\Debug x64\CSampleList.o" "Win32\Debug x64\CSeqScptList.o" "Win32\Debug x64\CSndDrv.o" "Win32\Debug x64\CSndLib.o" "Win32\Debug x64\CSndRam.o" "Win32\Debug x64\CSndScptList.o" "Win32\Debug x64\CSndSeq.o" "Win32\Debug x64\CSndStrHdl.o" "Win32\Debug x64\CWavData.o" "Win32\Debug x64\CWavDataList.o" "Win32\Debug x64\CWavDataStr.o" "Win32\Debug x64\dllmain.o" "Win32\Debug x64\misc\assert\myassert.o" "Win32\Debug x64\misc\CFindFile.o" "Win32\Debug x64\misc\CStrings.o" "Win32\Debug x64\misc\mydxerr.o" "Win32\Debug x64\SndInt.o"  -o sndlib.dll -Wl,--start-group -lksguid -Wl,--end-group  -lksguid -lSharedFunctions -lole32 -lwinmm -ldsound -lksguid -ldxerr8 -ldxerr9
Win32\Debug x64\CWavData.o: In function `CWavData::AnalyzeFormatChunk(_iobuf*, long)':
c:/MyProj/sndlib/CWavData.cpp:156: undefined reference to `_GUID const& __mingw_uuidof<KSDATAFORMAT_SUBTYPE_PCM_STRUCT>()'

I then tried this too

.. -o sndlib.dll -Wl,--start-group -lksguid -lSharedFunctions -lole32 -lwinmm -ldsound  -Wl,--end-group 

I also tried this

.. -Wl,--start-group -lksguid -Wl,--end-group -lSharedFunctions -lole32 -lwinmm -ldsound 

I am still getting the linker error. Any suggestions ?

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
James Franco
  • 4,516
  • 10
  • 38
  • 80
  • Why don't you use `-Wl,--start-group`, `-Wl,--end-group` to enclose all of the libraries you link to? – πάντα ῥεῖ Mar 11 '15 at 19:13
  • I tried this `-o sndlib.dll -Wl,--start-group, -lksguid -lSharedFunctions -lole32 -lwinmm -ldsound -Wl,--end-group ` but still same result – James Franco Mar 11 '15 at 19:16
  • You might try: -Wl,--start-group,-lksguid,-lSharedFunctions,-lole32,-lwinmm,-ldsound ,--end-group to be sure the linker sees the options in order. [yes, I'm guessing] – Dale Wilson Mar 11 '15 at 19:31
  • @DaleWilson _"yes, I'm guessing"_ Absolutely! This option is meant to make the order of appearance irrelevant :P ... – πάντα ῥεῖ Mar 11 '15 at 19:32
  • @πάνταῥεῖ Strangely enough sometimes when you run out of ideas, trying things that absolutely won't work reveals what the problem is. My next suggestion would be to try the ld command directly without getting gcc involved. – Dale Wilson Mar 11 '15 at 19:43

1 Answers1

0

"This makes me think that there is a linker error. "

C:/MyProj/sndlib/CWavData.cpp:156: undefined reference to `_GUID const& __mingw_uuidof<KSDATAFORMAT_SUBTYPE_PCM_STRUCT>()'
collect2.exe: error: ld returned 1 exit status

Of course that's a linker error, the ld appearing proves this. Order of object files and libraries matters, as they need to appear before used from another object file or library.

The way to overcome a specific order is needed to let the linker see the symbols, is to use the -Wl,--start-group and -Wl,--end-group options for GCC, you can use to create groups of libraries appearing within, without need for a particular order.


As for your comments and updates, you should try the following linker command line:

g++.exe -shared -Wl,--output-def=libsndlib.def -Wl,--out-implib=libsndlib.a 
    -Wl,--dll -LC:\mingw64\x86_64-w64-mingw32\lib  
    -Lc:\MyProj\SharedFunctions\SharedFunctions\ -L"Win32\Debug x64"  
    "Win32\Debug x64\ADPCM\CWavImaData.o" "Win32\Debug x64\CCategoryLst.o"  
    "Win32\Debug x64\CSampleList.o" "Win32\Debug x64\CSeqScptList.o"  
    "Win32\Debug x64\CSndDrv.o" "Win32\Debug x64\CSndLib.o"  
    "Win32\Debug x64\CSndRam.o" "Win32\Debug x64\CSndScptList.o"  
    "Win32\Debug x64\CSndSeq.o"  
    "Win32\Debug x64\CSndStrHdl.o" "Win32\Debug x64\CWavData.o" 
    "Win32\Debug x64\CWavDataList.o" "Win32\Debug x64\CWavDataStr.o"  
    "Win32\Debug x64\dllmain.o" "Win32\Debug x64\misc\assert\myassert.o"  
    "Win32\Debug x64\misc\CFindFile.o" "Win32\Debug x64\misc\CStrings.o"  
    "Win32\Debug x64\misc\mydxerr.o" "Win32\Debug x64\SndInt.o"      
    -Wl,--start-group 
    -lksguid  -lSharedFunctions -lole32 -lwinmm -ldsound  
    -ldxerr8 -ldxerr9 
    -Wl,--end-group 
    -o sndlib.dll
Community
  • 1
  • 1
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190