CMake链接失败 [英] CMake Linking Fails

查看:528
本文介绍了CMake链接失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为CMake配置项目,并且遇到链接问题-项目文件全部编译成功,然后它表示正在链接并报告未找到的各种符号.

I'm configuring my project for CMake and am having linking problems - the project files all compile successfully, then it says it is linking and reports all sorts of symbols not found.

这些符号大部分是由我自己的代码提供的,而其中一些符号是由BerkeleyDB提供的,BerkeleyDB的位置已正确放置并包含在内.

These symbols are mostly provided by my own code, while some of them are provided by BerkeleyDB, which is being properly located and included.

这是我的顶级CMakeLists.txt:

Here is my top-level CMakeLists.txt:

cmake_minimum_required(VERSION 2.6)

project( rpdb C )

#   add local modules path for project
set( CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/" )

#   where to look for source files (headers and source)
include_directories( include src )

#   define sub-directories of source that cmake knows about as well a where their output will be put
add_subdirectory( src bin )

#   compiler-dependent flags:
if( CMAKE_COMPILER_IS_GNUCC )
    #   gcc
    add_definitions( -ggdb -fsigned-char -freg-struct-return -Wall -W -Wshadow -Wstrict-prototypes -Wpointer-arith -Wcast-qual -Winline -Werror )
else( CMAKE_COMPILER_IS_GNUCC )
    #   non-gcc (intended for clang)
    add_definitions( -ggdb -fsigned-char -Wall -W -Wshadow -Wstrict-prototypes -Wpointer-arith -Wcast-qual -Winline -Werror )
endif( CMAKE_COMPILER_IS_GNUCC )


#   distribution configuration
set(CMAKE_C_FLAGS_DISTRIBUTION "-O3")
set(CMAKE_CXX_FLAGS_DISTRIBUTION "-O3")

这是我的src级CMakeLists.txt:

And here is my src-level CMakeLists.txt:

#   make sure we have libdb
find_package( BerkeleyDB REQUIRED )
include_directories( ${libdb_INCLUDE_DIRS} )
target_link_libraries( rpdb ${libdb_LIBRARIES} )

#   define variable specifying included source files - all .c files below this directory
file( GLOB rpdb_src "**/*.c" )

#   define shared library with sources
add_library( rpdb SHARED ${rpdb_src} )

输出(部分):

...
[100%] Building C object bin/CMakeFiles/rpdb.dir/RPDB_TransactionController/RPDB_TransactionController.c.o
Linking C shared library librpdb.dylib
Undefined symbols:
  "_RPDB_ReplicationVerbositySettingsController_displayMessageProcessingInformation", referenced from:
      _RPDB_SettingsController_internal_setVerbosity in RPDB_SettingsController.c.o
...

所有符号 do 实际上都存在.结果似乎发生在目标文件中的符号而不是当前正在查找的符号上.

All of the symbols do actually exist. The result seems to occur for symbols in object files other than the one it is currently looking at.

"cmake ../"的输出(从install,在顶层目录中):

The output from "cmake ../" (from install, a directory in the top-level):

=> cmake ..
-- Found BerkeleyDB: /usr/local/lib/libdb.dylib
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/ahaig/Projects/RPDB/RPDB_C/install

非常感谢您的帮助.

推荐答案

而不是GLOB,请尝试GLOB_RECURSE(

Instead of GLOB, try GLOB_RECURSE (CMake 2.8.3 docs, file command), which you can use like this: file ( GLOB_RECURSE rpdb_src "*.c" )

一个例子:

function (add_test_files)
    set ( src_files "" )

    ####################################################
    # Find all C/C++ files recursively from current dir
    ####################################################
    foreach ( ext IN ITEMS "cpp" "cxx" "cc" "c" )
        file ( GLOB_RECURSE _files "*.${ext}" )
        set ( src_files ${src_files} ${_files} )
    endforeach ()

    message ( STATUS "Found: ${src_files}" )

    add_executable ( test ${src_files} )
endfunction ()

add_test_files ()

这篇关于CMake链接失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆