title: Undefined reference to
body: <p>I am trying to use ASIO (Audio Stream Input/Output). I downloaded SDK, but because this SDK has no library, only headers and source files, I had to compile it into a library. Moreover, it was written so that it uses some of the features from MSVC (specifically some macro definitions - _WIN64 and WINVER). I cannot use MSVC for licensing reasons. So I am using Eclipse CDT with MinGW, told preprocessor to set these macros (for Windows 7 x64) and happily compiled a library.</p>
  
  <p>Now when I want to use that library, I keep getting Undefined reference. Can you please tell me what I am getting wrong?</p>
  
  <p>Hello world file (load ASIO driver and print its info):</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;asio.h&gt;
  
  int main(int argc, char **argv) {
      ASIODriverInfo driverInfo;
      ASIOInit(&amp;driverInfo);
  
      printf("ASIO version: %ld\n", driverInfo.asioVersion);
      printf("Driver name: %s\n", driverInfo.name);
      printf("Driver version: %ld\n", driverInfo.driverVersion);
      printf("Error message: %s\n", driverInfo.errorMessage);
  
      return (EXIT_SUCCESS);
  }
  </code></pre>
  
  <p>Verbose output from gcc:</p>
  
  <pre><code>&gt; gcc -v -Lc:/ASIOSDK2/lib/Win7/libasio.a -o ASIOHello.exe src\main.o
  &gt; Using built-in specs. COLLECT_GCC=gcc
  &gt; COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/4.6.1/lto-wrapper.exe Target: mingw32 Configured with: ../gcc-4.6.1/configure
  &gt; --enable-languages=c,c++,fortran,objc,obj-c++ --disable-sjlj-exceptions --with-dwarf2 --enable-shared --enable-libgomp --disable-win32-registry --enable-libstdcxx-debug --enable-version-specific-runtime-libs --build=mingw32 --prefix=/mingw Thread model: win32 gcc version 4.6.1 (GCC) 
  &gt; COMPILER_PATH=c:/mingw/bin/../libexec/gcc/mingw32/4.6.1/;c:/mingw/bin/../libexec/gcc/;c:/mingw/bin/../lib/gcc/mingw32/4.6.1/../../../../mingw32/bin/
  &gt; LIBRARY_PATH=c:/mingw/bin/../lib/gcc/mingw32/4.6.1/;c:/mingw/bin/../lib/gcc/;c:/mingw/bin/../lib/gcc/mingw32/4.6.1/../../../../mingw32/lib/;c:/mingw/bin/../lib/gcc/mingw32/4.6.1/../../../;/mingw/lib/
  &gt; COLLECT_GCC_OPTIONS='-v' '-Lc:/ASIOSDK2/lib/Win7/libasio.a' '-o'
  &gt; 'ASIOHello.exe' '-mtune=i386' '-march=i386' 
  &gt; c:/mingw/bin/../libexec/gcc/mingw32/4.6.1/collect2.exe -Bdynamic -o
  &gt; ASIOHello.exe c:/mingw/bin/../lib/gcc/mingw32/4.6.1/../../../crt2.o
  &gt; c:/mingw/bin/../lib/gcc/mingw32/4.6.1/crtbegin.o
  &gt; -Lc:/ASIOSDK2/lib/Win7/libasio.a -Lc:/mingw/bin/../lib/gcc/mingw32/4.6.1 -Lc:/mingw/bin/../lib/gcc -Lc:/mingw/bin/../lib/gcc/mingw32/4.6.1/../../../../mingw32/lib -Lc:/mingw/bin/../lib/gcc/mingw32/4.6.1/../../.. -L/mingw/lib src\main.o -lmingw32 -lgcc_eh -lgcc -lmoldname -lmingwex -lmsvcrt
  &gt; -ladvapi32 -lshell32 -luser32 -lkernel32 -lmingw32 -lgcc_eh -lgcc -lmoldname -lmingwex -lmsvcrt c:/mingw/bin/../lib/gcc/mingw32/4.6.1/crtend.o src\main.o: In function
  &gt; `main': &lt;my workspace path&gt;\ASIOHello\Debug/../src/main.c:7: undefined
  &gt; reference to `ASIOInit' collect2: ld returned 1 exit status Build
  &gt; error occurred, build is stopped Time consumed: 454  ms.
  </code></pre>
  
  <p>And output from nm for the library (only the relevant object):</p>
  
  <pre><code>&gt; asio.o:
  &gt; 00000000 b .bss
  &gt; 00000000 d .data
  &gt; 00000000 N .debug_abbrev
  &gt; 00000000 N .debug_aranges
  &gt; 00000000 N .debug_info
  &gt; 00000000 N .debug_line
  &gt; 00000000 N .debug_loc
  &gt; 00000000 N .debug_str
  &gt; 00000000 r .eh_frame
  &gt; 00000000 r .rdata
  &gt; 00000000 t .text
  &gt; 0000055a T __Z10ASIOFuturelPv
  &gt; 0000018c T __Z15ASIOGetChannelsPlS_
  &gt; 00000596 T __Z15ASIOOutputReadyv
  &gt; 0000052c T __Z16ASIOControlPanelv
  &gt; 000001db T __Z16ASIOGetLatenciesPlS_
  &gt; 0000029b T __Z17ASIOCanSampleRated
  &gt; 0000047b T __Z17ASIOCreateBuffersP14ASIOBufferInfollP13ASIOCallbacks
  &gt; 0000022a T __Z17ASIOGetBufferSizePlS_S_S_
  &gt; 000002e9 T __Z17ASIOGetSampleRatePd
  &gt; 0000031e T __Z17ASIOSetSampleRated
  &gt; 000004fe T __Z18ASIODisposeBuffersv
  &gt; 00000422 T __Z18ASIOGetChannelInfoP15ASIOChannelInfo
  &gt; 000003b1 T __Z18ASIOSetClockSourcel
  &gt; 0000036c T __Z19ASIOGetClockSourcesP15ASIOClockSourcePl
  &gt; 000003e6 T __Z21ASIOGetSamplePositionP11ASIOSamplesP13ASIOTimeStamp
  &gt; 00000103 T __Z8ASIOExitv
  &gt; 00000000 T __Z8ASIOInitP14ASIODriverInfo
  &gt; 0000015e T __Z8ASIOStopv
  &gt; 00000130 T __Z9ASIOStartv
  &gt;          U __ZN11AsioDrivers19removeCurrentDriverEv
  &gt;          U _asioDrivers
  &gt; 00000000 B _theAsioDriver
  &gt; 
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>Okay, now I managed to compile the library so it is accesible by C code (I added </p>
  
  <blockquote>
    <p>extern "C"</p>
  </blockquote>
  
  <p>in front of every function declaration. Now the nm output looks like this:</p>
  
  <pre><code>&gt; 0000036c T _ASIOGetClockSources
  &gt; 000001db T _ASIOGetLatencies
  &gt; 000003e6 T _ASIOGetSamplePosition
  &gt; 000002e9 T _ASIOGetSampleRate
  &gt; 00000000 T _ASIOInit
  &gt; 00000596 T _ASIOOutputReady
  &gt; 000003b1 T _ASIOSetClockSource
  &gt; 0000031e T _ASIOSetSampleRate
  &gt; 00000130 T _ASIOStart
  &gt; 0000015e T _ASIOStop
  </code></pre>
  
  <p>which is looking pretty good. I still have some name mangling tho, it still complains about undefined references. </p>
  
  <pre><code>gcc -v -Lc:/ASIOSDK2/lib/Win7 -lasio -o ASIOHello.exe src\main.o
  </code></pre>
  
  <p>What am I still missing here?</p>
  
  <p>EDIT 2:
  I finally made things work. It seems the error was caused by some leftover settings caching. Deleting the project and setting everything up again solved the problem. But thanks @John Zwick for the tip with library compilation, I would be stuck on that for a long time.</p>