如何在Windows上链接到libjpeg时解决“未定义的引用”?

时间:2014-03-29 12:41:17

标签: windows g++ cmake mingw libjpeg

我正在尝试构建一个软件,它需要libfftw3f,libxml2和libjpeg,以及Windows上的MinGW和CMake。

我已经通过mingw-get下载了libxml2的编译版本,并且自己从源代码构建了libfftw3f和libjpeg,没有任何问题。在CMake GUI中设置include和库路径后,将生成Makefile并mingw32-make编译所有源文件。但是当涉及链接二进制文件时,我收到以下错误消息:

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

上述文件JPEGImage.cpp包含jpeglib.h

CMake生成的link.txt显示以下行:

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

我想知道g ++试图链接到jpeg_std_error,而结果却是nm libjpeg.a,程序的名称是_jpeg_std_error(前导下划线似乎很常见于C Windows上的功能)。我已经尝试通过运行objcopy --remove-leading-char libjpeg.a libjpeg2.a来创建一个没有前导下划线的libjpeg版本(之后,我用新的libjpeg2.a替换了libjpeg.a)。但这并没有以任何方式改变mingw32-make的上述输出。

任何建议都会很棒。提前谢谢!


编辑 - 附加信息

CMakeLists.txt的相关摘录

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()

请注意,使用可用的libjpeg,libxml2和libfftw3包在Linux上构建它就像一个魅力。但是在Windows上,我必须在CMake GUI中自己设置目录和库的路径,特别是链接libjpeg很麻烦。

mingw32-make VERBOSE=1

的输出
"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

2 个答案:

答案 0 :(得分:2)

像这样包含jpeglib.h

extern "C" {
    #include <jpeglib.h>
}

答案 1 :(得分:0)

领先的下划线不是问题。对于你的lib路径

-LC:\ffld\jpeg-6b\.libs\libjpeg.a

应该是

-LC:\ffld\jpeg-6b\.libs

不确定如何使用CMake gui修复它,我想你只需再次运行它并选择目录而不是文件。编译器可能没有抱怨,因为它可能在路径中找到“jpeg.dll”,并试图链接到它,这可能与您尝试使用的版本不是二进制兼容的。