C / C ++库与CMake链接

时间:2015-06-17 03:37:29

标签: c++ c mongodb cmake

我需要在c ++项目中使用MongoDB C驱动程序。我想使用CMake,因为我使用的IDE与它很好地集成,但编译器不能识别MongoDB驱动程序

所以,我已经尝试了更多的东西,并为您提供更多信息。我制作了以下剧本:

cmake_minimum_required(VERSION 3.2)
project(sorting)

set(ENV{PKG_CONFIG_PATH} "/usr/lib64")
find_package(PkgConfig REQUIRED)

pkg_search_module(MONGO REQUIRED mongoc-1.0)


message(SEND_ERROR "A ${MONGO_FOUND}")        # Error:A 1
message(SEND_ERROR "B ${MONGO_LIBRARIES}")    # Error:B ssl;crypto;rt;mongoc-1.0;bson-1.0
message(SEND_ERROR "C ${MONGO_LIBRARY_DIRS}") # Error:C /usr/lib64
message(SEND_ERROR "D ${MONGO_LDFLAGS}")      # Error:D -L/usr/lib64;-lssl;-lcrypto;-lrt;-lmongoc-1.0;-lbson-1.0
message(SEND_ERROR "E ${MONGO_LDFLAGS_OTHER}")# Error:E
message(SEND_ERROR "F ${MONGO_INCLUDE_DIRS}") # Error:F /usr/include/libmongoc-1.0;/usr/include/libbson-1.0
message(SEND_ERROR "G ${MONGO_CFLAGS}")       # Error:G -I/usr/include/libmongoc-1.0;-I/usr/include/libbson-1.0
message(SEND_ERROR "H ${MONGO_CFLAGS_OTHER}") # Error:H


include_directories(${MONGO_LIBRARY_DIRS})
include_directories(${MONGO_INCLUDE_DIRS})

#link_directories(${MONGO_LIBRARY_DIRS})
#link_directories(${MONGO_INCLUDE_DIRS})


set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}   -std=c++11")
set(SOURCE_FILES main.cpp)

add_executable(sorting ${SOURCE_FILES})
target_link_libraries(sorting ${MONGO_LIBRARIES})

当我从我的cmake文件中删除所有message()函数并尝试构建运行include_directories未启用的项目时,我收到此错误:

error while loading shared libraries: libmongoc-1.0.so.0: cannot open shared object file: No such file or directory

当我评论include_directories并取消注释link_directories时,我收到此错误:

fatal error: mongoc.h: No such file or directory #include <mongoc.h>

另外,出于好奇,为什么如果我将MONGO中的名称pkg_search_module更改为MONGODBMONG,我会收到此错误:

Error:None of the required 'mongoc-1.0' found

3 个答案:

答案 0 :(得分:3)

你需要告诉cmake链接到monog库,这是通过

实现的
TARGET_LINK_LIBRARIES

命令,在您的特定情况下,它将在ADD_EXECUTABLE命令之后,并且与

完全相同
TARGET_LINK_LIBRARIES(sorting ${MONGO_DB_LIBRARIES})

此外,您的PKG_SEARCH_MODULE似乎有误,似乎应该是

PKG_SEARCH_MODULE(MONGO_DB REQUIRED libmongoc-1.0)

答案 1 :(得分:3)

我能够编译使用

给出的mongo示例代码
cmake_minimum_required(VERSION 2.8)
project(sorting)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)

include_directories(/usr/include/libmongoc-1.0 /usr/include/libbson-1.0)

find_library(MONGODB_LIBRARY mongoc-1.0)
find_library(BSON_LIBRARY bson-1.0)

add_executable(sorting ${SOURCE_FILES})
target_link_libraries(sorting ${MONGODB_LIBRARY} ${BSON_LIBRARY})

你可以试试这个CMakeFiles.txt吗?

BTW它存在一个用于c ++项目的mongo驱动程序c ++!

答案 2 :(得分:3)

我终于能够做到了! 我的IDE(CLION)正在覆盖环境变量LD_LIBRARY_PATH。我在IDE设置中设置它指向库目录,现在一切正常。 这是我使用的配置:

cmake_minimum_required(VERSION 3.2)
project(sorting C)


set(ENV{PKG_CONFIG_PATH} "")
find_package(PkgConfig REQUIRED)

pkg_search_module(MONGO REQUIRED mongoc)

include_directories(${MONGO_INCLUDE_DIRS})
include_directories(${MONGO_LIBRARY_DIRS})

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.c)

add_executable(sorting ${SOURCE_FILES})
target_link_libraries(sorting ${MONGO_LIBRARIES} )