I am trying to test mongodb c++ driver on Ubuntu 16.04.
The driver is installed in ${Devfolder}/sdk/mongodb/
and the test is in ${Devfolder}/testMongoDb/.
The code was compliled and tested using:
export PKG_CONFIG_PATH=${Devfolder}/sdk/mongodb/lib/pkgconfig
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${Devfolder}/sdk/mongodb/lib
c++ --std=c++11 test.cpp -o test $(pkg-config --cflags --libs libmongocxx)
Then I want to use mongodb driver in my CMake project.
ugitho#ugitho:projects$ tree -L 3
.
├── sdk
│ └── mongodb
│ ├── bin
│ ├── include
│ ├── lib
│ └── share
├── testMongo
│ ├── a.out
│ ├── build
│ │ ├── CMakeCache.txt
│ │ ├── CMakeFiles
│ │ ├── cmake_install.cmake
│ │ ├── cmongodb
│ │ └── Makefile
│ ├── CMakeLists.txt
│ ├── test
│ └── test.cpp
But I got the following error:
ugitho#ugitho:build$ cmake ..
LIBMONGOCXX_INCLUDE_DIRS = /home/ugitho/NGUYENKHAC/projects/sdk/mongodb/include/mongocxx/v_noabi;/home/ugitho/NGUYENKHAC/projects/sdk/mongodb/include/bsoncxx/v_noabi
LIBMONGOCXX_LIBRARIES = LIBMONGOCXX_LIBRARY_PATH-NOTFOUND;LIBBSONCXX_LIBRARY_PATH-NOTFOUND
LIBBSONCXX_INCLUDE_DIRS = /home/ugitho/NGUYENKHAC/projects/sdk/mongodb/include/bsoncxx/v_noabi
LIBBSONCXX_LIBRARIES = LIBBSONCXX_LIBRARY_PATH-NOTFOUND
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
LIBBSONCXX_LIBRARY_PATH
linked by target "cmongodb" in directory
/home/ugitho/NGUYENKHAC/projects/testMongoDb
linked by target "cmongodb" in directory
/home/ugitho/NGUYENKHAC/projects/testMongoDb
LIBMONGOCXX_LIBRARY_PATH
linked by target "cmongodb" in directory
/home/ugitho/NGUYENKHAC/projects/testMongoDb
-- Configuring incomplete, errors occurred!
See also "/home/ugitho/NGUYENKHAC/projects/testMongoDb/build/CMakeFiles/CMakeOutput.log".
edited*:
Here are CMakeLists.txt and test.cpp.
CMakeLists.txt:
cmake_minimum_required(VERSION 3.2)
project(cmongodb)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_PREFIX_PATH /home/ugitho/NGUYENKHAC/projects/sdk/mongodb)
#find_package(libmongocxx REQUIRED)
#find_package(libbsoncxx REQUIRED)
find_package(PkgConfig REQUIRED)
pkg_check_modules(LIBMONGOCXX REQUIRED libmongocxx)
pkg_check_modules(LIBBSONCXX REQUIRED libbsoncxx)
message("LIBMONGOCXX_INCLUDE_DIRS = ${LIBMONGOCXX_INCLUDE_DIRS}")
message("LIBMONGOCXX_LIBRARIES = ${LIBMONGOCXX_LIBRARIES}")
message("LIBBSONCXX_INCLUDE_DIRS = ${LIBBSONCXX_INCLUDE_DIRS}")
message("LIBBSONCXX_LIBRARIES = ${LIBBSONCXX_LIBRARIES}")
set(COMMON_LIBRARIES ${LIBMONGOCXX_LIBRARIES} ${LIBBSONCXX_LIBRARIES})
set(SOURCE_FILES test.cpp)
add_executable(cmongodb ${SOURCE_FILES})
target_include_directories(cmongodb PUBLIC ${LIBMONGOCXX_INCLUDE_DIRS})
target_include_directories(cmongodb PUBLIC ${LIBBSONCXX_INCLUDE_DIRS})
target_link_libraries(cmongodb ${COMMON_LIBRARIES})
test.cpp:
#include <iostream>
#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
int main(int, char **) {
std::cout << "1" << std::endl;
mongocxx::instance inst{};
mongocxx::client conn{mongocxx::uri{}};
bsoncxx::builder::stream::document document{};
auto collection = conn["testdb"]["testcollection"];
document << "hello"
<< "world";
collection.insert_one(document.view());
auto cursor = collection.find({});
for (auto &&doc : cursor) {
std::cout << bsoncxx::to_json(doc) << std::endl;
}
return 0;
}
Do you have any idea about what I missed here?
Updates:
with the new CMakeLists.txt, there are no more cmake errors.
ugitho#ugitho:build$ cmake ..
-- Checking for module 'libmongocxx'
-- Found libmongocxx, version 3.4.0
-- Checking for module 'libbsoncxx'
-- Found libbsoncxx, version 3.4.0
CMAKE_PREFIX_PATH = /home/ugitho/NGUYENKHAC/projects/sdk/mongodb/shared
LIBMONGOCXX_INCLUDE_DIRS = /home/ugitho/NGUYENKHAC/projects/sdk/mongodb/include/mongocxx/v_noabi;/home/ugitho/NGUYENKHAC/projects/sdk/mongodb/include/bsoncxx/v_noabi
LIBMONGOCXX_LIBRARIES = mongocxx;bsoncxx
LIBBSONCXX_INCLUDE_DIRS = /home/ugitho/NGUYENKHAC/projects/sdk/mongodb/include/bsoncxx/v_noabi
LIBBSONCXX_LIBRARIES = bsoncxx
-- Configuring done
-- Generating done
-- Build files have been written to: /home/ugitho/NGUYENKHAC/projects/testMongoDb/build
[1]+ Done gedit /home/ugitho/NGUYENKHAC/projects/testMongoDb/build/CMakeFiles/CMakeOutput.log
The errors are now on make level:
ugitho#ugitho:build$ make
Scanning dependencies of target cmongodb
[ 50%] Building CXX object CMakeFiles/cmongodb.dir/test.cpp.o
[100%] Linking CXX executable cmongodb
/opt/llvm/x86_64/6.0.0.g631a/bin/ld: error: unable to find library -lmongocxx
/opt/llvm/x86_64/6.0.0.g631a/bin/ld: error: unable to find library -lbsoncxx
/opt/llvm/x86_64/6.0.0.g631a/bin/ld: error: unable to find library -lbsoncxx
collect2: error: ld returned 1 exit status
make[2]: *** [cmongodb] Error 1
make[1]: *** [CMakeFiles/cmongodb.dir/all] Error 2
make: *** [all] Error 2
UPDATE *** 27/12/2018
cmake_minimum_required(VERSION 3.2)
project(cmongodb)
set(CMAKE_CXX_STANDARD 14)
get_filename_component(PARENT_DIR ${CMAKE_CURRENT_SOURCE_DIR} DIRECTORY)
set(CMAKE_PREFIX_PATH ${PARENT_DIR}/sdk/mongodb)
message("CMAKE_PREFIX_PATH = ${CMAKE_PREFIX_PATH}")
find_package(libmongocxx REQUIRED)
find_package(libbsoncxx REQUIRED)
set(MongoDb_INCLUDE_DIR ${PARENT_DIR}/sdk/mongodb/include)
include_directories(${MongoDb_INCLUDE_DIR}/mongocxx/v_noabi
${MongoDb_INCLUDE_DIR}/bsoncxx/v_noabi)
set(SOURCE_FILES test.cpp)
add_executable(cmongodb ${SOURCE_FILES})
target_link_libraries(cmongodb ${LIBMONGOCXX_LIBRARIES}
${LIBBSONCXX_LIBRARIES})
I managed to compile using this CMakeLists.txt.
new project config will be coming soon.
{GTest & Boost & MongoDb C++ Driver}
Thank you!!
Related
Installing nested libraries brings compilation errors.
I try to have a project called CROSP that contains multiple internal libraries, namely : StrainParameterisation, RodProperties, PolynomialRepresentation. These libraries are then linked to one base library : CROSP.
This one is the library that is then installed in my usr/local/...
The problem is that when I try to include the library from one external package, I get some errors saying that the compiler did not found the .so file for the internal libraries.
In the following I detail the topology of my project.
The layout of the project is the following:
.
├── cmake
│ ├── cmake_uninstall.cmake.in
│ ├── Config.cmake.in
│ └── installation_module.cmake
├── CMakeLists.txt
└── src
├── CMakeLists.txt
├── CROSP
│ └── cosserat_rod.cpp
├── include
│ └── CROSP
│ ├── CROSP
│ │ └── cosserat_rod.hpp
│ ├── polynomial_representation
│ │ └── polynomial_representation.hpp
│ ├── rod_properties
│ │ └── rod_properties.hpp
│ └── strain_parameterisation
│ └── strain_parameterisation.hpp
├── polynomial_representation
│ └── polynomial_representation.cpp
├── rod_properties
│ └── rod_properties.cpp
└── strain_parameterisation
└── strain_parameterisation.cpp
The CMakeLists are :
Top level CMakeLists
cmake_minimum_required(VERSION 3.22 FATAL_ERROR)
project(CROSP LANGUAGES CXX VERSION 2.0)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED True)
find_package(Eigen3 3.4 NO_MODULE REQUIRED)
# External utilities to configure the package
include(GNUInstallDirs)
set(INCLUDE_INSTALL_DIR ${CMAKE_INSTALL_INCLUDEDIR})
set(LIB_INSTALL_DIR ${CMAKE_INSTALL_LIBDIR})
# Give different names for debug and Release
set(CMAKE_RELEASE_POSTFIX "")
set(CMAKE_DEBUG_POSTFIX "-debug")
add_subdirectory(src)
# Install the library using the default routine
include(cmake/installation_module.cmake)
The installation_module.cmake
# Generates ${PROJECT_NAME}Config.cmake file to use our package in other projects
include(CMakePackageConfigHelpers)
configure_package_config_file(
cmake/Config.cmake.in # input template
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake # output config file
INSTALL_DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake # where to put the config file during install
PATH_VARS INCLUDE_INSTALL_DIR LIB_INSTALL_DIR # paths to be used
NO_CHECK_REQUIRED_COMPONENTS_MACRO
)
# Generates a config file to ensure that URL's version is checked when importing it
write_basic_package_version_file(
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
VERSION ${PROJECT_VERSION}
COMPATIBILITY SameMajorVersion
)
# When running make install, config files should be copied as well
install(
FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake
)
# Add the possibility tu run 'make uninstall' to remove files added via 'make install'
configure_file(
cmake/cmake_uninstall.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake
IMMEDIATE #ONLY
)
add_custom_target(uninstall COMMAND ${CMAKE_COMMAND} -P "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake")
The Config.cmake.in module :
#PACKAGE_INIT#
set(#PROJECT_NAME#_VERSION #PROJECT_VERSION#)
set_and_check(#PROJECT_NAME#_INCLUDE_DIRS #PACKAGE_INCLUDE_INSTALL_DIR#/#PROJECT_NAME#)
set_and_check(#PROJECT_NAME#_LIBRARY_DIR #PACKAGE_LIB_INSTALL_DIR#)
find_library(#PROJECT_NAME#_LIBRARIES NAMES #PROJECT_NAME# PATHS ${#PROJECT_NAME#_LIBRARY_DIR} NO_DEFAULT_PATH)
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(#PROJECT_NAME# DEFAULT_MSG #PROJECT_NAME#_INCLUDE_DIRS #PROJECT_NAME#_LIBRARIES)
Then We have the CMakeLists in src
include_directories(include)
add_library(PolynomialRepresentation SHARED
include/${PROJECT_NAME}/polynomial_representation/polynomial_representation.hpp
polynomial_representation/polynomial_representation.cpp
)
target_link_libraries(PolynomialRepresentation
PRIVATE
Eigen3::Eigen
)
target_compile_options(PolynomialRepresentation
PRIVATE
-Wall
-Wextra
)
add_library(RodProperties SHARED
include/${PROJECT_NAME}/rod_properties/rod_properties.hpp
rod_properties/rod_properties.cpp
)
target_link_libraries(RodProperties
PRIVATE
Eigen3::Eigen
PolynomialRepresentation
)
target_compile_options(RodProperties
PRIVATE
-Wall
-Wextra
)
add_library(StrainParameterisation SHARED
include/${PROJECT_NAME}/strain_parameterisation/strain_parameterisation.hpp
strain_parameterisation/strain_parameterisation.cpp
)
target_link_libraries(StrainParameterisation
PRIVATE
Eigen3::Eigen
PolynomialRepresentation
)
target_compile_options(RodProperties
PRIVATE
-Wall
-Wextra
)
add_library(${PROJECT_NAME} SHARED
include/${PROJECT_NAME}/${PROJECT_NAME}/cosserat_rod.hpp
${PROJECT_NAME}/cosserat_rod.cpp
)
target_link_libraries(${PROJECT_NAME}
PRIVATE
Eigen3::Eigen
PUBLIC
StrainParameterisation
RodProperties
PolynomialRepresentation
)
target_compile_options(RodProperties
PRIVATE
-Werror
-Wall
-Wextra
)
add_executable(main main.cpp)
target_link_libraries(main ${PROJECT_NAME} Eigen3::Eigen)
install(
TARGETS
${PROJECT_NAME}
DESTINATION
${CMAKE_INSTALL_LIBDIR}
)
install(DIRECTORY include/${PROJECT_NAME}
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
I can run and install this library.
The following code compiles and runs fine.
#include "CROSP/CROSP/cosserat_rod.hpp"
int main(int argc, char *argv[])
{
::CROSP::polynomial_representation::PolynomialRepresentation poly(5);
::CROSP::rod_properties::MaterialProperties material(200, 100, 10);
::CROSP::CosseratRod rod(poly, material);
::CROSP::CosseratRod rod2(15);
::CROSP::CosseratRod rod3;
return 0;
}
The installation in my usr folder has the following layout
usr
└── local
├── include
│ └── CROSP
│ ├── CROSP
│ │ └── cosserat_rod.hpp
│ ├── polynomial_representation
│ │ └── polynomial_representation.hpp
│ ├── rod_properties
│ │ └── rod_properties.hpp
│ └── strain_parameterisation
│ └── strain_parameterisation.hpp
├── lib
│ ├── libCROSP-debug.so
│ └── libCROSP.so
└── share
└── CROSP
└── cmake
├── CROSPConfig.cmake
└── CROSPConfigVersion.cmake
From another project, I can include the library without problem.
Here there is an example
cmake_minimum_required(VERSION 3.5)
project(test_crosp_library LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(CROSP REQUIRED)
find_package(Eigen3 3.4 NO_MODULE REQUIRED)
add_executable(test_crosp_library main.cpp)
target_link_libraries(test_crosp_library
PUBLIC
CROSP
Eigen3::Eigen
)
And the main.cpp looks like this
#include "CROSP/CROSP/cosserat_rod.hpp"
int main()
{
::CROSP::CosseratRod rod; // errors
return 0;
}
I get the following errors
[ 50%] Linking CXX executable test_crosp_library
/usr/bin/ld: warning: libStrainParameterisation.so, needed by /usr/local/lib/libCROSP.so, not found (try using -rpath or -rpath-link)
/usr/bin/ld: warning: libRodProperties.so, needed by /usr/local/lib/libCROSP.so, not found (try using -rpath or -rpath-link)
/usr/bin/ld: /usr/local/lib/libCROSP.so: undefined reference to `LieAlgebra::SE3Pose::getRotationMatrix() const'
/usr/bin/ld: /usr/local/lib/libCROSP.so: undefined reference to `CROSP::strain_parameterisation::StrainParameterisation::StrainParameterisation(CROSP::polynomial_representation::PolynomialRepresentation, unsigned int)'
/usr/bin/ld: /usr/local/lib/libCROSP.so: undefined reference to `CROSP::strain_parameterisation::StrainParameterisation::StrainParameterisation(CROSP::polynomial_representation::PolynomialRepresentation, Eigen::Matrix<double, -1, 1, 0, -1, 1> const&, unsigned int)'
/usr/bin/ld: /usr/local/lib/libCROSP.so: undefined reference to `CROSP::rod_properties::RodProperties::RodProperties(CROSP::polynomial_representation::PolynomialRepresentation)'
/usr/bin/ld: /usr/local/lib/libCROSP.so: undefined reference to `CROSP::strain_parameterisation::StrainParameterisation::updateStacks(Eigen::Matrix<double, -1, 1, 0, -1, 1> const&, Eigen::Matrix<double, -1, 1, 0, -1, 1> const&, Eigen::Matrix<double, -1, 1, 0, -1, 1> const&)'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/test_crosp_library.dir/build.make:97: test_crosp_library] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/test_crosp_library.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
Can you tell what is wrong ?
Moreover, I would like to know if I can use the INTERFACE functionality in order to install the internal libraries in my usr/local with namespaces.
So the CMakeLists.txt of a package using CROSP will be:
find_package(CROSP REQUIRED)
add_executable(main main.cpp)
target_link_libraries(main
CROSP::MaterialProperties
CROSP::StrainParameterisation
)
The project structure is as below
├── ast
│ ├── CMakeLists.txt
│ ├── expression
│ │ ├── CMakeLists.txt
│ │ ├── Expression.cpp
│ │ ├── Expression.hpp
│ │ ├── NumericExpression.cpp
│ │ └── NumericExpression.h
│ ├── Visitor.cpp
│ └── Visitor.hpp
├── callSlang
│ ├── CMakeLists.txt
│ └── main.cpp
├── CMakeLists.txt
├── contexts
│ ├── CMakeLists.txt
│ ├── Context.hpp
│ ├── Symbol.cpp
│ ├── Symbol.hpp
│ └── SymbolTable.hpp
├── frontend
│ ├── CMakeLists.txt
│ ├── Parser.cpp
│ └── Parser.hpp
├── Makefile
├── meta
│ ├── CMakeLists.txt
│ └── Meta.hpp
└── README.md
CMakeLists.txt
cmake_minimum_required(VERSION 3.16.3)
# set the project name
# https://stackoverflow.com/questions/71740678/cmake-error-in-findterminfo-with-clang-15-on-macos
project(slangLLvm VERSION 1.0 LANGUAGES C CXX)
# specify the C++ standard
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_BUILD_TYPE "Debug")
set(LLVM_DIR /usr/lib/llvm-14/lib/cmake/llvm)
find_package(LLVM REQUIRED CONFIG
PATHS ${search_paths}
NO_DEFAULT_PATH)
add_subdirectory(meta)
add_subdirectory(contexts)
add_subdirectory(ast)
add_subdirectory(frontend)
add_subdirectory(callSlang)
callSlang/CMakeLists.txt
set(SOURCE_FILE main.cpp)
# add the files for creating executable
add_executable(slangLLVM ${SOURCE_FILE})
#link the libs
target_link_libraries(slangLLVM
PRIVATE
ast
frontend
)
ast/CMakeLists.txt
set(SOURCE_FILES Visitor.cpp)
add_library(ast ${SOURCE_FILES})
target_link_libraries(ast
PUBLIC
meta
contexts
LLVMSupport
expression
PRIVATE
frontend
)
# get the include dirs
target_include_directories(ast
INTERFACE
.
PUBLIC
${LLVM_INCLUDE_DIRS}
${PROJECT_SOURCE_DIR}/meta
${PROJECT_SOURCE_DIR}/ast/expression
)
add_subdirectory(expression)
expression/CMakeLists.txt
set(SOURCE_FILES Expression.cpp NumericExpression.cpp)
add_library(experssion ${SOURCE_FILES})
target_link_libraries(experssion
PUBLIC
ast
)
# get the include dirs
target_include_directories(experssion
INTERFACE
.
PUBLIC
${LLVM_INCLUDE_DIRS}
${PROJECT_SOURCE_DIR}/contexts
${PROJECT_SOURCE_DIR}/ast
${PROJECT_SOURCE_DIR}/meta
)
# INTERFACE tells to use includes to other libs but on the ast
frontend/CMakeLists.txt
set(SOURCE_FILES Parser.cpp)
add_library(frontend STATIC ${SOURCE_FILES})
target_link_libraries(frontend
PRIVATE
ast
)
# get the include dirs
target_include_directories(frontend
INTERFACE
.
PRIVATE
${PROJECT_BINARY_DIR}
${PROJECT_SOURCE_DIR}/ast
${PROJECT_SOURCE_DIR}/contexts
${PROJECT_SOURCE_DIR}/ast/expression
)
meta/CMakeLists.txt
add_library(meta INTERFACE)
# get the include dirs
target_include_directories(meta INTERFACE .)
This compiles without error but fails during linking with the following error
[100%] Linking CXX executable slangLLVM
/usr/bin/ld: cannot find -lexpression
collect2: error: ld returned 1 exit status
make[3]: *** [callSlang/CMakeFiles/slangLLVM.dir/build.make:93: callSlang/slangLLVM] Error 1
make[3]: Leaving directory '/home/nithin/learn/c-cpp/slang-llvm/build'
make[2]: *** [CMakeFiles/Makefile2:392: callSlang/CMakeFiles/slangLLVM.dir/all] Error 2
make[2]: Leaving directory '/home/nithin/learn/c-cpp/slang-llvm/build'
make[1]: *** [Makefile:84: all] Error 2
make[1]: Leaving directory '/home/nithin/learn/c-cpp/slang-llvm/build'
make: *** [Makefile:4: all] Error 2
CallSlang depends on ast, ast depends on the expression and vice versa.
I have tried the solution in the link but it not-working so far for me. I am really new to CMake. Is it caused by the circular call by ast and expression, if so how can correct the same?
The complete code can be found in this link
Could someone please tell me the possible cause and solution? If I've missed out anything, over- or under-emphasized a specific point, please let me know in the comments. Thank you so much in advance for your time.
I am trying to first create a static library and then link it to an executable using CMake. My project file structure looks like this:
├── CMakeLists.txt
├── build
├── lib
│ ├── CMakeLists.txt
│ ├── build
│ ├── include
│ │ └── Point.hpp
│ └── src
│ └── Point.cpp
└── mainApp.cpp
I first build the library like so.
cmake_minimum_required(VERSION 2.8.9)
project(CAST3)
set(CMAKE_BUILD_TYPE Release)
include_directories(include)
file(GLOB SOURCES "src/*.cpp")
add_library(CAST3 STATIC ${SOURCES})
However, when i try to link the library to my executable I get an error.
This is my executable
#include"Point.hpp"
int main(int argc, char *argv[]){
Point p = Point(1,2,3);
return 0;
}
This is my CMake file to link the library to the executable.
cmake_minimum_required(VERSION 2.8.9)
project (CAST3)
set ( PROJECT_LINK_LIBS libCAST3.a )
link_directories( ${CMAKE_CURRENT_SOURCE_DIR}/lib/build)
add_executable(libtest mainApp.cpp)
target_link_libraries(libtest ${PROJECT_LINK_LIBS} )
When I run that I get the this error
/mainApp.cpp:1:9: fatal error: 'Point.hpp' file not found
#include"Point.hpp"
^~~~~~~~~~~
1 error generated.
make[2]: *** [CMakeFiles/libtest.dir/mainApp.cpp.o] Error 1
make[1]: *** [CMakeFiles/libtest.dir/all] Error 2
make: *** [all] Error 2
What am I missing?
What am I missing?
You are missing target_include_directories(CAST3 PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include) inside lib/CMakeLists.txt.
You are missing add_subdirectory(lib) from root CMakeLists.txt.
The set ( PROJECT_LINK_LIBS libCAST3.a ) and target_link_libraries(libtest ${PROJECT_LINK_LIBS} ) and link_directories( ${CMAKE_CURRENT_SOURCE_DIR}/lib/build) could be removed. Then you are missing just target_link_libraries(libtest PUBLIC CAST3). Cmake will automatically find the proper .a file and propagate include paths with target_link_libraries.
So your lib/CMakeLists.txt could look like this:
cmake_minimum_required(VERSION 3.0) # I would advise to update
project(CAST3)
include_directories(include)
file(GLOB sources src/*.cpp) # upper case variable names reserved for exported variables by convention
add_library(CAST3 ${sources}) # STATIC by default
target_include_directories(CAST3 PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
And root CMakeLists.txt could look like this:
cmake_minimum_required(VERSION 3.0) # I would advise to update
project(CAST3)
add_subdirectory(lib)
add_executable(libtest mainApp.cpp)
target_link_libraries(libtest CAST3)
RT~ ps: cmake version 3.9.2
My codebase just like this.
suzanwen#n224-004-133:~/repos/C++/ttt:)$ tree -L 2
.
├── build
│ ├── bin
│ ├── CMakeCache.txt
│ ├── CMakeFiles
│ ├── cmake_install.cmake
│ ├── lib
│ ├── Makefile
│ ├── test
│ └── thirdparty
├── build.sh
├── CMakeLists.txt
├── Makefile
├── test
│ ├── CMakeLists.txt
│ └── main.cc
└── thirdparty
├── CMakeLists.txt
├── gflags
└── hellolib
10 directories, 9 files
my thirdparty/hellolib/CMakeLists.txt is
PROJECT(hello)
SET(LIBHELLO_SRC hello.cc)
MESSAGE(STATUS "LIBRARY PATH=" ${LIBRARY_OUTPUT_PATH})
ADD_LIBRARY(hello_static STATIC ${LIBHELLO_SRC})
SET_TARGET_PROPERTIES(hello_static PROPERTIES ARCHIVE_OUTPUT_NAME "hello")
my test/CMakeLists.txt is
INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/thirdparty/hellolib
${PROJECT_SOURCE_DIR}/thirdparty/gflags/include)
IF(LIBRARY_OUTPUT_PATH)
LINK_DIRECTORIES(${LIBRARY_OUTPUT_PATH})
ENDIF(LIBRARY_OUTPUT_PATH)
ADD_EXECUTABLE(main main.cc)
TARGET_LINK_LIBRARIES(main hello)
# TARGET_LINK_LIBRARIES(main hello_static)
when I build my top-level project, an error occurs like this.
/usr/bin/c++ -rdynamic CMakeFiles/main.dir/main.cc.o -o ../bin/main -L/home/suzanwen/repos/C++/ttt/build/lib -Wl,-rpath,/home/suzanwen/repos/C++/ttt/build/lib -lhello
/usr/bin/ld: cannot find -lhello
But when I comment the line # SET_TARGET_PROPERTIES(hello_static PROPERTIES ARCHIVE_OUTPUT_NAME "hello") and TARGET_LINK_LIBRARIES with hello_static, everything goes fine.
It seems that TARGET_LINK_LIBRARIES cannot find renamed lib target. Could anyone explain it? thanks in advance.
It seems that TARGET_LINK_LIBRARIES cannot find renamed lib target.
Setting ARCHIVE_OUTPUT_NAME property renames not a target, but an output file. So linking with a target still works:
TARGET_LINK_LIBRARIES(main hello_static)
One cannot rename the target once it is created, but it is possible to create ALIAS for a target:
ADD_LIBRARY(hello ALIAS hello_static)
After that it is possible to link with the alias:
TARGET_LINK_LIBRARIES(main hello)
I am learning to use cmake for a project using a shared library, but I keep getting this error:
Linking CXX executable test/test/robot_test
/usr/bin/ld: cannot open output file test/test/robot_test: No such file or directory
collect2: error: ld returned 1 exit status
make[2]: *** [test/test/robot_test] Error 1
make[1]: *** [CMakeFiles/test/robot_test.dir/all] Error 2
make: *** [all] Error 2
Here is my CMake file:
cmake_minimum_required(VERSION 2.8.12)
project("Particle Filter")
set(CMAKE_BUILD_TYPE Release)
include_directories(include)
set(LIB_SOURCES src/robot.cc src/sampler.cc src/general.cc)
set(LIB_HEADERS include/robot.h include/sampler.h include/general.h)
add_library(my_lib SHARED ${LIB_SOURCES} ${LIB_HEADERS})
install(TARGETS my_lib DESTINATION lib)
set(APP_SOURCES test/robot_test.cc test/sampler_test.cc)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/test")
foreach(test ${APP_SOURCES})
#cut off .cc of src files using empty string
string(REPLACE ".cc" "" testname ${test})
add_executable(${testname} ${test})
target_link_libraries(${testname} my_lib)
endforeach(test ${APP_SOURCES})
add_definitions(
-std=c++11 # Or -std=c++0x
# Other flags
)
Here is my tree (excluding the build directory that contains a lot of this such as my makefile,.so and .a file):
├── CMakeLists.txt
├── driver.cc
├── include
│ ├── general.h
│ ├── robot.h
│ └── sampler.h
├── lib
├── notes
├── src
│ ├── general.cc
│ ├── robot.cc
│ └── sampler.cc
└── test
├── robot_test.cc
└── sampler_test.cc
Also, the .so or .a files are not getting saved to my lib folder after sudo make install, how do I fix this?
The first parameter for command add_executable is not a filename but a name of target. Target's name shouldn't contain special symbols like slash ("/").
You may control output directory of executables created by setting variable CMAKE_RUNTIME_OUTPUT_DIRECTORY:
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/test")
...
# Relative path to executable will be 'test/robot_test'.
add_executable(robot_test robot_test.cc)