title: How to resolve "undefined reference" when linking to libjpeg on Windows?
body: <p>I'm trying to build a software, which requires libfftw3f, libxml2 and libjpeg, with MinGW and CMake on Windows.</p>
  
  <p>I have downloaded a compiled version of libxml2 via <code>mingw-get</code> and built libfftw3f and libjpeg from source by myself without any problem. After setting the include and library paths in the CMake GUI, the Makefile is generated and <code>mingw32-make</code> compiles all source files. But when it comes to linking the binary, I get the following error messages:</p>
  
  <pre><code>CMakeFiles\detect_ffld.dir/objects.a(JPEGImage.cpp.obj):JPEGImage.cpp:(.text+0x1
  90): undefined reference to `jpeg_std_error(jpeg_error_mgr*)'
  CMakeFiles\detect_ffld.dir/objects.a(JPEGImage.cpp.obj):JPEGImage.cpp:(.text+0x1
  b4): undefined reference to `jpeg_CreateCompress(jpeg_compress_struct*, int, uns
  igned int)'
  CMakeFiles\detect_ffld.dir/objects.a(JPEGImage.cpp.obj):JPEGImage.cpp:(.text+0x1
  c6): undefined reference to `jpeg_stdio_dest(jpeg_compress_struct*, _iobuf*)'
  CMakeFiles\detect_ffld.dir/objects.a(JPEGImage.cpp.obj):JPEGImage.cpp:(.text+0x1
  f6): undefined reference to `jpeg_set_defaults(jpeg_compress_struct*)'
  CMakeFiles\detect_ffld.dir/objects.a(JPEGImage.cpp.obj):JPEGImage.cpp:(.text+0x2
  0d): undefined reference to `jpeg_set_quality(jpeg_compress_struct*, int, int)'
  CMakeFiles\detect_ffld.dir/objects.a(JPEGImage.cpp.obj):JPEGImage.cpp:(.text+0x2
  1d): undefined reference to `jpeg_start_compress(jpeg_compress_struct*, int)'
  CMakeFiles\detect_ffld.dir/objects.a(JPEGImage.cpp.obj):JPEGImage.cpp:(.text+0x2
  58): undefined reference to `jpeg_write_scanlines(jpeg_compress_struct*, unsigne
  d char**, unsigned int)'
  CMakeFiles\detect_ffld.dir/objects.a(JPEGImage.cpp.obj):JPEGImage.cpp:(.text+0x2
  66): undefined reference to `jpeg_finish_compress(jpeg_compress_struct*)'
  CMakeFiles\detect_ffld.dir/objects.a(JPEGImage.cpp.obj):JPEGImage.cpp:(.text+0x2
  e5): undefined reference to `jpeg_std_error(jpeg_error_mgr*)'
  CMakeFiles\detect_ffld.dir/objects.a(JPEGImage.cpp.obj):JPEGImage.cpp:(.text+0x3
  09): undefined reference to `jpeg_CreateDecompress(jpeg_decompress_struct*, int,
   unsigned int)'
  CMakeFiles\detect_ffld.dir/objects.a(JPEGImage.cpp.obj):JPEGImage.cpp:(.text+0x3
  1b): undefined reference to `jpeg_stdio_src(jpeg_decompress_struct*, _iobuf*)'
  CMakeFiles\detect_ffld.dir/objects.a(JPEGImage.cpp.obj):JPEGImage.cpp:(.text+0x3
  2b): undefined reference to `jpeg_read_header(jpeg_decompress_struct*, int)'
  CMakeFiles\detect_ffld.dir/objects.a(JPEGImage.cpp.obj):JPEGImage.cpp:(.text+0x3
  59): undefined reference to `jpeg_start_decompress(jpeg_decompress_struct*)'
  CMakeFiles\detect_ffld.dir/objects.a(JPEGImage.cpp.obj):JPEGImage.cpp:(.text+0x3
  ff): undefined reference to `jpeg_read_scanlines(jpeg_decompress_struct*, unsign
  ed char**, unsigned int)'
  CMakeFiles\detect_ffld.dir/objects.a(JPEGImage.cpp.obj):JPEGImage.cpp:(.text+0x4
  38): undefined reference to `jpeg_finish_decompress(jpeg_decompress_struct*)'
  collect2: ld returned 1 exit status
  mingw32-make[2]: *** [detect_ffld.exe] Error 1
  mingw32-make[1]: *** [CMakeFiles/detect_ffld.dir/all] Error 2
  mingw32-make: *** [all] Error 2
  </code></pre>
  
  <p>The mentioned file <code>JPEGImage.cpp</code> includes <code>jpeglib.h</code>.</p>
  
  <p>My <code>link.txt</code> generated by CMake shows the following line:</p>
  
  <pre><code>C:\PROGRA~2\MinGW\bin\g++.exe   -fopenmp -O3 -DNDEBUG    -fopenmp -Wl,--whole-archive CMakeFiles\detect_ffld.dir/objects.a -Wl,--no-whole-archive  -o detect_ffld.exe -Wl,--out-implib,libdetect_ffld.dll.a -Wl,--major-image-version,0,--minor-image-version,0  -LC:\ffld\fftw-3.3.4\.libs -LC:\ffld\jpeg-6b\.libs -L"C:\Program Files (x86)\MinGW\msys\1.0\lib" -lfftw3f -ljpeg -lxml2 -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32
  </code></pre>
  
  <p>I wondered about g++ trying to link to <code>jpeg_std_error</code>, while it turns out by running <code>nm libjpeg.a</code>, that the name of the procedure is <code>_jpeg_std_error</code> (leading underscore seems to be common for C functions on Windows). I have already tried to create a version of libjpeg without the leading underscore by running <code>objcopy --remove-leading-char libjpeg.a libjpeg2.a</code> (after that, I replaced libjpeg.a by the new libjpeg2.a, of course). But that didn't change the above output of <code>mingw32-make</code> in any way.</p>
  
  <p>Any advice would be great. Thanks in advance!</p>
  
  <hr>
  
  <h2>Edit - Additional Information</h2>
  
  <h3>Relevant excerpts from CMakeLists.txt</h3>
  
  <pre><code>CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
  
  PROJECT(FFLD)
  
  SET(HEADERS Rectangle.h JPEGImage.h Object.h Scene.h HOGPyramid.h Patchwork.h Model.h Mixture.h Intersector.h)
  SET(SOURCES Rectangle.cpp JPEGImage.cpp Object.cpp Scene.cpp HOGPyramid.cpp
  Patchwork.cpp Model.cpp Mixture.cpp)
  ADD_EXECUTABLE(detect_ffld ${HEADERS} ${SOURCES} ffld.cpp)
  
  # There are no CMake FFTW package, so find it ourselves
  FIND_PATH(FFTW3_INCLUDE_DIR fftw3.h)
  IF(FFLD_HOGPYRAMID_DOUBLE)
    FIND_LIBRARY(FFTW3_LIBRARIES fftw3)
  ELSE()
    FIND_LIBRARY(FFTW3_LIBRARIES fftw3f)
  ENDIF()
  IF(NOT FFTW3_INCLUDE_DIR OR NOT FFTW3_LIBRARIES)
    MESSAGE(FATAL_ERROR "Could not find fftw3.")
  ENDIF()
  INCLUDE_DIRECTORIES(${FFTW3_INCLUDE_DIR})
  TARGET_LINK_LIBRARIES(detect_ffld ${FFTW3_LIBRARIES})
  
  FIND_PACKAGE(JPEG REQUIRED)
  IF(JPEG_FOUND)
    INCLUDE_DIRECTORIES(${JPEG_INCLUDE_DIR})
    TARGET_LINK_LIBRARIES(detect_ffld ${JPEG_LIBRARIES})
  ENDIF()
  
  FIND_PACKAGE(LibXml2 REQUIRED)
  IF(LIBXML2_FOUND)
    INCLUDE_DIRECTORIES(${LIBXML2_INCLUDE_DIR})
    TARGET_LINK_LIBRARIES(detect_ffld ${LIBXML2_LIBRARIES})
    ADD_DEFINITIONS(${LIBXML2_DEFINITIONS})
  ENDIF()
  
  FIND_PACKAGE(OpenMP)
  IF(OPENMP_FOUND)
    SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
    SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_C_FLAGS}")
  ENDIF()
  </code></pre>
  
  <p>Note that building this on Linux using the available libjpeg, libxml2 and libfftw3 packages works like a charm. But on Windows, I had to set the paths to the directories and libraries by myself in the CMake GUI and especially linking libjpeg is troublesome.</p>
  
  <h3>Output of <code>mingw32-make VERBOSE=1</code></h3>
  
  <pre><code>"C:\Program Files (x86)\CMake\bin\cmake.exe" -HC:\ffld\ffld -BC:\ffld\ffld\build
   --check-build-system CMakeFiles\Makefile.cmake 0
  mingw32-make -f CMakeFiles\Makefile2 detect_ffld
  mingw32-make[1]: Entering directory 'C:/ffld/ffld/build'
  "C:\Program Files (x86)\CMake\bin\cmake.exe" -HC:\ffld\ffld -BC:\ffld\ffld\build
   --check-build-system CMakeFiles\Makefile.cmake 0
  "C:\Program Files (x86)\CMake\bin\cmake.exe" -E cmake_progress_start C:\ffld\ffl
  d\build\CMakeFiles 9
  mingw32-make -f CMakeFiles\Makefile2 CMakeFiles/detect_ffld.dir/all
  mingw32-make[2]: Entering directory 'C:/ffld/ffld/build'
  mingw32-make -f CMakeFiles\detect_ffld.dir\build.make CMakeFiles/detect_ffld.dir
  /depend
  mingw32-make[3]: Entering directory 'C:/ffld/ffld/build'
  "C:\Program Files (x86)\CMake\bin\cmake.exe" -E cmake_depends "MinGW Makefiles"
  C:\ffld\ffld C:\ffld\ffld C:\ffld\ffld\build C:\ffld\ffld\build C:\ffld\ffld\bui
  ld\CMakeFiles\detect_ffld.dir\DependInfo.cmake --color=
  mingw32-make[3]: Leaving directory 'C:/ffld/ffld/build'
  mingw32-make -f CMakeFiles\detect_ffld.dir\build.make CMakeFiles/detect_ffld.dir
  /build
  mingw32-make[3]: Entering directory 'C:/ffld/ffld/build'
  Linking CXX executable detect_ffld.exe
  "C:\Program Files (x86)\CMake\bin\cmake.exe" -E cmake_link_script CMakeFiles\det
  ect_ffld.dir\link.txt --verbose=1
  "C:\Program Files (x86)\CMake\bin\cmake.exe" -E remove -f CMakeFiles\detect_ffld
  .dir/objects.a
  C:\PROGRA~2\MinGW\bin\ar.exe cr CMakeFiles\detect_ffld.dir/objects.a @CMakeFiles
  \detect_ffld.dir\objects1.rsp
  C:\PROGRA~2\MinGW\bin\G__~1.EXE   -fopenmp -O3 -DNDEBUG    -fopenmp -Wl,--whole-
  archive CMakeFiles\detect_ffld.dir/objects.a -Wl,--no-whole-archive  -o detect_f
  fld.exe -Wl,--out-implib,libdetect_ffld.dll.a -Wl,--major-image-version,0,--mino
  r-image-version,0  -LC:\ffld\fftw-3.3.4\.libs -LC:\ffld\jpeg-6b -L"C:\Program Fi
  les (x86)\MinGW\msys\1.0\lib" -lfftw3f -ljpeg -lxml2 -lkernel32 -luser32 -lgdi32
   -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32
  CMakeFiles\detect_ffld.dir/objects.a(JPEGImage.cpp.obj):JPEGImage.cpp:(.text+0x3
  f0): undefined reference to `jpeg_std_error(jpeg_error_mgr*)'
  CMakeFiles\detect_ffld.dir/objects.a(JPEGImage.cpp.obj):JPEGImage.cpp:(.text+0x4
  16): undefined reference to `jpeg_CreateDecompress(jpeg_decompress_struct*, int,
   unsigned int)'
  CMakeFiles\detect_ffld.dir/objects.a(JPEGImage.cpp.obj):JPEGImage.cpp:(.text+0x4
  22): undefined reference to `jpeg_stdio_src(jpeg_decompress_struct*, _iobuf*)'
  CMakeFiles\detect_ffld.dir/objects.a(JPEGImage.cpp.obj):JPEGImage.cpp:(.text+0x4
  32): undefined reference to `jpeg_read_header(jpeg_decompress_struct*, int)'
  CMakeFiles\detect_ffld.dir/objects.a(JPEGImage.cpp.obj):JPEGImage.cpp:(.text+0x4
  62): undefined reference to `jpeg_start_decompress(jpeg_decompress_struct*)'
  CMakeFiles\detect_ffld.dir/objects.a(JPEGImage.cpp.obj):JPEGImage.cpp:(.text+0x5
  00): undefined reference to `jpeg_read_scanlines(jpeg_decompress_struct*, unsign
  ed char**, unsigned int)'
  CMakeFiles\detect_ffld.dir/objects.a(JPEGImage.cpp.obj):JPEGImage.cpp:(.text+0x5
  3a): undefined reference to `jpeg_finish_decompress(jpeg_decompress_struct*)'
  CMakeFiles\detect_ffld.dir/objects.a(JPEGImage.cpp.obj):JPEGImage.cpp:(.text+0x7
  20): undefined reference to `jpeg_std_error(jpeg_error_mgr*)'
  CMakeFiles\detect_ffld.dir/objects.a(JPEGImage.cpp.obj):JPEGImage.cpp:(.text+0x7
  43): undefined reference to `jpeg_CreateCompress(jpeg_compress_struct*, int, uns
  igned int)'
  CMakeFiles\detect_ffld.dir/objects.a(JPEGImage.cpp.obj):JPEGImage.cpp:(.text+0x7
  53): undefined reference to `jpeg_stdio_dest(jpeg_compress_struct*, _iobuf*)'
  CMakeFiles\detect_ffld.dir/objects.a(JPEGImage.cpp.obj):JPEGImage.cpp:(.text+0x7
  8e): undefined reference to `jpeg_set_defaults(jpeg_compress_struct*)'
  CMakeFiles\detect_ffld.dir/objects.a(JPEGImage.cpp.obj):JPEGImage.cpp:(.text+0x7
  a9): undefined reference to `jpeg_set_quality(jpeg_compress_struct*, int, int)'
  CMakeFiles\detect_ffld.dir/objects.a(JPEGImage.cpp.obj):JPEGImage.cpp:(.text+0x7
  b9): undefined reference to `jpeg_start_compress(jpeg_compress_struct*, int)'
  CMakeFiles\detect_ffld.dir/objects.a(JPEGImage.cpp.obj):JPEGImage.cpp:(.text+0x7
  e8): undefined reference to `jpeg_write_scanlines(jpeg_compress_struct*, unsigne
  d char**, unsigned int)'
  CMakeFiles\detect_ffld.dir/objects.a(JPEGImage.cpp.obj):JPEGImage.cpp:(.text+0x7
  f5): undefined reference to `jpeg_finish_compress(jpeg_compress_struct*)'
  c:/progra~2/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: C
  MakeFiles\detect_ffld.dir/objects.a(JPEGImage.cpp.obj): bad reloc address 0x64 i
  n section `.text$_ZNSt6vectorIhSaIhEE14_M_fill_insertEN9__gnu_cxx17__normal_iter
  atorIPhS1_EEjRKh[__ZNSt6vectorIhSaIhEE14_M_fill_insertEN9__gnu_cxx17__normal_ite
  ratorIPhS1_EEjRKh]'
  collect2.exe: error: ld returned 1 exit status
  CMakeFiles\detect_ffld.dir\build.make:300: recipe for target 'detect_ffld.exe' f
  ailed
  mingw32-make[3]: *** [detect_ffld.exe] Error 1
  mingw32-make[3]: Leaving directory 'C:/ffld/ffld/build'
  CMakeFiles\Makefile2:97: recipe for target 'CMakeFiles/detect_ffld.dir/all' fail
  ed
  mingw32-make[2]: *** [CMakeFiles/detect_ffld.dir/all] Error 2
  mingw32-make[2]: Leaving directory 'C:/ffld/ffld/build'
  CMakeFiles\Makefile2:109: recipe for target 'CMakeFiles/detect_ffld.dir/rule' fa
  iled
  mingw32-make[1]: *** [CMakeFiles/detect_ffld.dir/rule] Error 2
  mingw32-make[1]: Leaving directory 'C:/ffld/ffld/build'
  Makefile:121: recipe for target 'detect_ffld' failed
  mingw32-make: *** [detect_ffld] Error 2
  </code></pre>