我正在尝试使用find_library和target_link_libraries链接Windows中的静态库(libsndfile)。 find_library命令有效(可以通过消息进行验证),但是运行make(使用MinGW / mingw32-make)时,仍然得到未定义的引用。
我在CMakeLists.txt文件中做错什么了吗,还是我还缺少其他内容?
cmake_minimum_required(VERSION 3.0.0)
# Project name
project(ClickDetect)
include_directories(
.
D:/libs/libsndfile/include
)
# Tried adding this:
#link_directories (D:/libs/libsndfile/lib)
# Define the C sources
set ( OSLCODA_SRCS
${CMAKE_CURRENT_LIST_DIR}/ALT/find_click.c
${CMAKE_CURRENT_LIST_DIR}/ALT/four1.c
${CMAKE_CURRENT_LIST_DIR}/src/osl_coda.c
)
SET_SOURCE_FILES_PROPERTIES( ${OSLCODA_SRCS} PROPERTIES LANGUAGE C )
#Tried this way:
#SET_TARGET_PROPERTIES(SNDFILE PROPERTIES
# IMPORTED_LOCATION D:/libs/libsndfile/lib/libsndfile-1.a)
# This seems to find the library... either .a or .lib
find_library(SNDFILE NAMES "libsndfile-1.a"
PATHS "D:/libs/libsndfile/lib" NO_DEFAULT_PATH)
# Prints "SNDFILE: D:/libs/libsndfile/lib/libsndfile-1.a"
if(NOT SNDFILE)
message(FATAL_ERROR "libsndfile Not Found!")
else()
message(STATUS "SNDFILE: ${SNDFILE}")
endif()
add_executable(oslcoda ${OSLCODA_SRCS})
target_link_libraries(oslcoda ${SNDFILE} )
输出:
D:\FORCE\ClickDetect>cmake D:\FORCE\ClickDetect -G "MinGW Makefiles" -DCMAKE_BUILD_TYPE=Release -BD:\FORCE\ClickDetect/cmake-build
-- The C compiler identification is GNU 5.3.0
-- The CXX compiler identification is GNU 5.3.0
-- Check for working C compiler: C:/Qt5.7/Tools/mingw530_32/bin/gcc.exe
-- Check for working C compiler: C:/Qt5.7/Tools/mingw530_32/bin/gcc.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: C:/Qt5.7/Tools/mingw530_32/bin/g++.exe
-- Check for working CXX compiler: C:/Qt5.7/Tools/mingw530_32/bin/g++.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- SNDFILE: D:/libs/libsndfile/lib/libsndfile-1.a
-- Configuring done
-- Generating done
-- Build files have been written to: D:/FORCE/ClickDetect/cmake-build
D:\FORCE\ClickDetect>cd cmake-build
D:\FORCE\ClickDetect\cmake-build>mingw32-make VERBOSE=1
"C:\Program Files (x86)\CMake\bin\cmake.exe" -HD:\FORCE\ClickDetect -BD:\FORCE\ClickDetect\cmake-build --check-build-system CMakeFiles\Makefile.cmake 0
"C:\Program Files (x86)\CMake\bin\cmake.exe" -E cmake_progress_start D:\FORCE\ClickDetect\cmake-build\CMakeFiles D:\FORCE\ClickDetect\cmake-build\CMakeFiles\progress.marks
mingw32-make -f CMakeFiles\Makefile2 all
mingw32-make[1]: Entering directory 'D:/FORCE/ClickDetect/cmake-build'
mingw32-make -f CMakeFiles\oslcoda.dir\build.make CMakeFiles/oslcoda.dir/depend
mingw32-make[2]: Entering directory 'D:/FORCE/ClickDetect/cmake-build'
"C:\Program Files (x86)\CMake\bin\cmake.exe" -E cmake_depends "MinGW Makefiles" D:\FORCE\ClickDetect D:\FORCE\ClickDetect D:\FORCE\ClickDetect\cmake-build D:\FORCE\ClickDetect\cmake-build D:\FORCE\ClickDetect\cmake-build\CMakeFiles\oslcoda.dir\DependInfo.cmake --color=
mingw32-make[2]: Leaving directory 'D:/FORCE/ClickDetect/cmake-build'
mingw32-make -f CMakeFiles\oslcoda.dir\build.make CMakeFiles/oslcoda.dir/build
mingw32-make[2]: Entering directory 'D:/FORCE/ClickDetect/cmake-build'
[ 25%] Linking C executable oslcoda.exe
"C:\Program Files (x86)\CMake\bin\cmake.exe" -E cmake_link_script CMakeFiles\oslcoda.dir\link.txt --verbose=1
"C:\Program Files (x86)\CMake\bin\cmake.exe" -E remove -f CMakeFiles\oslcoda.dir/objects.a
C:\Qt5.7\Tools\mingw530_32\bin\ar.exe cr CMakeFiles\oslcoda.dir/objects.a @CMakeFiles\oslcoda.dir\objects1.rsp
C:\Qt5.7\Tools\mingw530_32\bin\gcc.exe -O3 -DNDEBUG -Wl,--whole-archive CMakeFiles\oslcoda.dir/objects.a -Wl,--no-whole-archive -o oslcoda.exe -Wl,--out-implib,liboslcoda.dll.a -Wl,--major-image-version,0,--minor-image-version,0 @CMakeFiles\oslcoda.dir\linklibs.rsp
CMakeFiles\oslcoda.dir/objects.a(osl_coda.c.obj):osl_coda.c:(.text.startup+0x1c9): undefined reference to `sf_open'
CMakeFiles\oslcoda.dir/objects.a(osl_coda.c.obj):osl_coda.c:(.text.startup+0x2a4): undefined reference to `sf_read_int'
CMakeFiles\oslcoda.dir/objects.a(osl_coda.c.obj):osl_coda.c:(.text.startup+0x3b7): undefined reference to `sf_read_int'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\oslcoda.dir\build.make:151: recipe for target 'oslcoda.exe' failed
mingw32-make[2]: *** [oslcoda.exe] Error 1
mingw32-make[2]: Leaving directory 'D:/FORCE/ClickDetect/cmake-build'
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/oslcoda.dir/all' failed
mingw32-make[1]: *** [CMakeFiles/oslcoda.dir/all] Error 2
mingw32-make[1]: Leaving directory 'D:/FORCE/ClickDetect/cmake-build'
Makefile:82: recipe for target 'all' failed
mingw32-make: *** [all] Error 2
编辑:
linklibs.rsp的内容:
D:/libs/libsndfile/lib/libsndfile-1.a -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32
更新
Tsyvarev是正确的,错误似乎根本与cmake无关,我只是不了解CMake如何选择其工具链。如评论中所建议,我手动运行了link命令,并得到了相同的错误。事实证明,原始构建是使用MSYS / MinGW完成的,而我和我的Qt MinGW都在我的路径上。 CMake使用的是我的Qt安装程序,我认为这很好,但是它不起作用(我猜是因为它是lib的64位版本)。
因此,在对路径以及CC和CXX变量进行了很多努力之后,我使用MSYS mingw64进行了链接。我仍然不确定我是否了解如何正确切换工具链。我尝试仅使用CC和CXX来设置编译器的位置,但是如果您的系统路径上有多个工具链,情况就会变得很混乱。最后,我必须将CC和CXX留为空白,只是确保MSYS路径在路径上我的Qt安装之前,以便CMake进行拾取。这可能会破坏我其他项目的构建。