I am trying to link log4cpp to my project. I use CMake and i can't figure out a way to do so.
Log4cpp is install on projectfolder/log4cpp/ (with bin/ include/ lib/ ... in it)
I use the following Findlog4cpp.cmake :
IF (LOG4CPP_FOUND)
SET(LOG4CPP_FIND_QUIETLY TRUE)
ENDIF (LOG4CPP_FOUND)
FIND_PATH(LOG4CPP_INCLUDE_DIR log4cpp/FileAppender.hh
"./log4cpp/include/"
)
FIND_LIBRARY(LOG4CPP_LIBRARIES
NAMES liblog4cpp.so
PATHS "./log4cpp/lib"
)
SET(LOG4CPP_FOUND 0)
IF(LOG4CPP_INCLUDE_DIR)
IF(LOG4CPP_LIBRARIES)
SET(LOG4CPP_FOUND 1 CACHE INTERNAL "log4cpp found")
IF (NOT LOG4CPP_FIND_QUIETLY)
MESSAGE(STATUS "Found Log4CPP")
ENDIF (NOT LOG4CPP_FIND_QUIETLY)
ENDIF(LOG4CPP_LIBRARIES)
ENDIF(LOG4CPP_INCLUDE_DIR)
MARK_AS_ADVANCED(
LOG4CPP_INCLUDE_DIR
LOG4CPP_LIBRARIES
)
and in my CMakeLists.txt i call it :
...
FIND_PACKAGE(log4cpp REQUIRED)
INCLUDE_DIRECTORIES(${LOG4CPP_INCLUDE_DIR})
SET(LIBS ${LOG4CPP_LIBRARIES} ${LIBS})
MESSAGE("############################# ${LOG4CPP_LIBRARIES}")
MESSAGE("############################# ${LOG4CPP_INCLUDE_DIR}")
...
The output :
-- Found Log4CPP
############################# /SOMEPATH/projectfolder/log4cpp/lib/liblog4cpp.so
############################# /SOMEPATH/projectfolder/log4cpp/include
-- Configuring done
-- Generating done
-- Build files have been written to: /SOMEPATH/projectfolder/
Then when i run make i have the folowing errors :
/SOMEPATH/projectfolder/common/Common.h:24:31: error: log4cpp/Category.hh: No such file or directory
/SOMEPATH/projectfolder/common/Common.h:25:35: error: log4cpp/FileAppender.hh: No such file or directory
/SOMEPATH/projectfolder/common/Common.h:26:34: error: log4cpp/BasicLayout.hh: No such file or directory
i just included the headers in common.h ( #include "log4cpp/xxx.hh" )
I add that all the build and compile works fine (with cmake then make) without log4cpp
Any help would be greatly appreciated
If you're adding your common directory as a subdirectory with its own CMakeLists.txt, you need to call INCLUDE_DIRECTORIES before you call ADD_SUBDIRECTORY if you want the directories already included to be passed down.
Related
I am attempting to integrate a C++ library I downloaded into a new project using CMake.
I downloaded the library ("downloaded_library"), created the build folder, and inside of it ran cmake .. and make. These both ran successfully, and than I navigated to the build folder and ran ./example to run the example file that came with the library. This also was successful, and so I hoped to add it to another project.
I added this working project into the 'libraries' folder of the following directory structure:
project
-libraries
-downloaded_library
-build
-include
-downloaded_lib
-downloaded_lib.h
-src
-examples
-example.cpp
-CMakeLists.txt
-src
-main.cpp
-build
-CMakeLists.txt
I hope to run the same code that ran in example.cpp in main.cpp, but I have been unable to get the import working. I navigate to the build folder and cmake .. runs successfully, but 'make' fails with an import error (fatal error on the include line, can't find the header file). This makes sense, as I didn't expect the same include line to work (I copy and pasted example.cpp to main.cpp), but I'm not sure what to make that.
fatal error: downloaded_lib/downloaded_lib.h: No such file or directory
1 | #include "downloaded_lib/downloaded_lib.h"
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
In this situation, what should my CMakeLists.txt contain, and what should my #include contain to be able to use the contents of this library in main.cpp in the same way I can in example.cpp?
EDIT---
I changed the package names above for simplicity sake, but the repository of 'downloaded_library' is the following: https://github.com/siposcsaba89/socketcan-cpp.
My top level CMakeLists.txt looks like this:
# Minimum version of CMake required to build this project
cmake_minimum_required(VERSION 3.0)
# Name of the project
project(projectname)
# Add all the source files needed to build the executable
add_executable(${PROJECT_NAME} src/main.cpp)
add_subdirectory(libraries/downloaded_library)
# Link the executable and the library together
target_link_libraries(${PROJECT_NAME} downloaded_library)
Edit 2-- (Here I will use the original package name, socketcan-cpp).
Top level CMakeLists.txt:
# Minimum version of CMake required to build this project
cmake_minimum_required(VERSION 3.0)
# Name of the project
project(socketcan_demo)
# Add all the source files needed to build the executable
add_executable(${PROJECT_NAME} src/main.cpp)
add_subdirectory("libraries/socketcan-cpp")
target_include_directories(${PROJECT_NAME} PUBLIC "libraries/socketcan-cpp/include")
When running make I get this error:
fatal error: socketcan_cpp/socketcan_cpp_export.h: No such file or directory
4 | #include <socketcan_cpp/socketcan_cpp_export.h>
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
So the top level CMakeLists is able to find the header file located in include, but that header file contains the line: #include <socketcan_cpp/socketcan_cpp_export.h> which references a file in a gen directory inside the build directory created by the library's CMakeLists.txt (https://github.com/siposcsaba89/socketcan-cpp/blob/master/CMakeLists.txt). And my top level package is unable to find it.
In order to include the header files from the downloaded library, you can add the following to the CMakeLists.txt file:
find_library(downloaded_lib
NAMES downloaded_lib
HINTS "path to downloaded lib file")
if(NOT downloaded_lib)
message(FATAL_ERROR "downloaded_lib not found!")
endif()
target_include_directories(${PROJECT_EXECUTABLE} PUBLIC "path to download_lib.h")
target_link_libraries(${PROJECT_EXECUTABLE} ${downloaded_lib})
This includes the directory where the header file is located in addition to the library file.
I have simple C++ project that is organized like this:
project
|-- Input
| |-- data.cpp <-- this file used eigen3
| |--- CmakeLists.txt
|--- main.cpp
|--- CmakeLists.txt
Basically I am trying to create .so library from input and have main.cpp calls functions in it.
CMake under project looks like this
cmake_minimum_required(VERSION 3.20)
project(myProj)
find_package (Eigen3 REQUIRED)
# Dependencies paths
set(EIGEN_INC_DIR ${EIGEN3_INCLUDE_DIR})
if (TARGET Eigen3::Eigen)
message("Eigen was found"). <--- I do see this message so Eigen3 package is found
endif (TARGET Eigen3::Eigen)
add_subdirectory(Input)
<more cmake commands to link with main.cpp>
CMake under Input looks like this
cmake_minimum_required(VERSION 3.20)
set(TARGET_LIB_INPUT input_data)
set(SRC_FILES
Data.cpp
)
set(INC_DIRS
${EIGEN_INC_DIR}
)
include_directories(${INC_DIRS})
add_library (${TARGET_LIB_INPUT} SHARED ${SRC_FILES})
target_link_libraries (${TARGET_LIB_INPUT} Eigen3::Eigen)
in file data.cpp, I do the following include to eigen3
#include <eigen3/Eigen/Core>
But I keep getting the error
fatal error: 'eigen3/Eigen/Core' file not found
I see the build command clearly include eigen directory:
-I /usr/local/include/eigen3
Anybody knows what am I missing here?
Thanks for help
The include directory defined in Eigen3::Eigen already includes eigen3 (e.g., /usr/include/eigen3 on Ubuntu). So you should use #include <Eigen/Core>.
You can check this by:
find_package(Eigen3 REQUIRED CONFIG)
# checking property of the target
get_target_property(inc_dir Eigen3::Eigen INTERFACE_INCLUDE_DIRECTORIES)
message("[DEBUG] inc_dir: ${inc_dir}")
# or checking the Eigen variable
message("[DEBUG] EIGEN3_INCLUDE_DIRS: ${EIGEN3_INCLUDE_DIRS}")
EIGEN3_INCLUDE_DIRS is defined in here.
I am somewhat new to cmake and completely new to glew, glfw, and the like. I'm following a youtube channel to learn more about game engines and programming.
My problem is linking the static glew library in my project using cmake.
First, I start with the glew source code from http://mcs.une.edu.au/doc/glew-devel/index.html.
I compile it by:
cd build; cmake ./cmake; make glew_s
This adds a lib directory into the build directory with libGLEW.a
A shortened version of my CMakeLists.txt looks like:
cmake_minimum_required (VERSION 3.5 FATAL_ERROR)
project (TestProject CXX)
set(CMAKE_CXX_FLAGS "-std=c++11")
###########################
# GLEW
###########################
add_subdirectory(${CMAKE_SOURCE_DIR}/Dependencies/GLEW)
target_link_libraries(${PROJECT_NAME} glew)
and in Dependencies/GLEW I have another CMakeLists.txt:
# Add glew source and header files
file(GLOB_RECURSE glew-lib ${CMAKE_CURRENT_SOURCE_DIR}/lib/*)
file(GLOB_RECURSE glew-headers ${CMAKE_CURRENT_SOURCE_DIR}/include/GL/*)
add_library(glew ${glew-lib} ${glew-headers})
target_include_directories(glew PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include/GL")
I put a copy of the libGLEW.a file into the lib directory and the include directory is copied from the glew source code include directory. It holds the GL directory, which contains the header files glew.h, wglew.h, eglew.h, and glxew.h.
When I run cmake I get the error:
CMake Error: Cannot determine link language for target "glew".
CMake Error: CMake can not determine linker language for target: glew
The glew source code also has a src directory with glew.c in it, but if I put it in the libs directory and include it in the Dependencies/GLEW/CMakeLists.txt like:
# Add glew source and header files
file(GLOB_RECURSE glew-lib ${CMAKE_CURRENT_SOURCE_DIR}/lib/*.c)
file(GLOB_RECURSE glew-headers ${CMAKE_CURRENT_SOURCE_DIR}/include/GL/*)
add_library(glew ${glew-lib} ${glew-headers})
target_include_directories(glew PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")
I get the error:
CMake Error: Error required internal CMake variable not set, cmake may
be not be built correctly.
Missing variable is: CMAKE_C_COMPILE_OBJECT
CMake Error: Error required internal CMake variable not set, cmake may
be not be built correctly.
Missing variable is: CMAKE_C_CREATE_STATIC_LIBRARY
Lastly, I have tried just including the glew.c and headers in the root CMakeLists.txt like:
#########################
# GLEW
#########################
include_directories("${CMAKE_SOURCE_DIR}/Dependencies/GLEW/lib/")
include_directories("${CMAKE_SOURCE_DIR}/Dependencies/GLEW/include/GL/")
Here cmake will finish, but it won't be able to compile, saying classes do not name a type and/or are not declared in this scope.
Any help would be appreciated. I was under the impression that the libGLEW.a was the static library, and all I would have to do is link and compile it along with the headers, but that did not work.
First of all, I forgot to use
make install
after using make the first time, so I was using the incorrect libGLEW.a file.
After including the proper libGLEW.a file, my directory structure had
./Dependencies/GLEW/include/GL/*.h //header files
./Dependencies/GLEW/lib/libGLEW.a //source file
Finally, the TopLevel CMakeLists.txt is changed to include:
add_library(glew STATIC IMPORTED GLOBAL)
set_target_properties(glew PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/Dependencies/GLEW/lib/libGLEW.a )
set_target_properties(glew PROPERTIES INCLUDE_DIRECTORIES ${CMAKE_SOURCE_DIR}/Dependencies/GLEW/include )
If I want to include the library from a lower CMakeLists.txt such as /Dependencies/GLEW/CMakeLists.txt Then I would have to first export the library from there and then import it at the topLevel CMakeLists.txt file.
Now, in order to use glew headers in my project I can just use #include .
First Attempt
In my cmake/c++ project I get the following error when compiling:
C:\local\projects\synergy-usb\synergy-through-usb-master>cmake .
You have called ADD_LIBRARY for library cryptopp without any source files. This typically indicates a problem with your CMakeLists.txt file
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:
LIBUSB_1_INCLUDE_DIR
used as include directory in directory C:/local/projects/synergy-usb/synergy-through-usb-master/src/lib/arch
used as include directory in directory C:/local/projects/synergy-usb/synergy-through-usb-master/src/lib/net
LIBUSB_1_LIBRARY
linked by target "arch" in directory C:/local/projects/synergy-usb/synergy-through-usb-master/src/lib/arch
-- Configuring incomplete, errors occurred!
See also "C:/local/projects/synergy-usb/synergy-through-usb-master/CMakeFiles/CMakeOutput.log".
So I am missing libusb libraries.
Second Attempt
So now I have found a version of libusb_1 (libusbx-1.0.18-win) which includes the folders:
examples
include
MinGW32
MinGW64
MS32
MS64
I copied MS64/dll (windows 64-bit) contents and put it here:
C:\local\libs\libusbx
I added this path to my PATH variable and then tried to run cmake again:
C:>cd local\projects\synergy-usb\synergy-through-usb-master
C:\local\projects\synergy-usb\synergy-through-usb-master>cmake .
You have called ADD_LIBRARY for library cryptopp without any source files. This typically indicates a problem with your CMakeLists.txt file
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:
LIBUSB_1_LIBRARY
linked by target "arch" in directory C:/local/projects/synergy-usb/synergy-through-usb-master/src/lib/arch
-- Configuring incomplete, errors occurred!
See also "C:/local/projects/synergy-usb/synergy-through-usb-master/CMakeFiles/CMakeOutput.log".
Now I am stuck again. I have no idea what it wants me to do. It knows where libusb is...
Edit
Here is the contects (part of) the CMakeLists.txt that has libusb in it:
find_package(libusb-1.0 REQUIRED)
set(inc
.
../base
../common
../mt
../platform
../synergy
${LIBUSB_1_INCLUDE_DIRS}
)
if (UNIX)
list(APPEND inc
../../..
../arch
)
endif()
include_directories(${inc})
add_library(arch STATIC ${src})
set(libs
../lib
${LIBUSB_1_LIBRARIES}
)
if (WIN32)
if (GAME_DEVICE_SUPPORT)
list(APPEND libs synxinhk)
endif()
endif()
target_link_libraries(arch ${libs})
Edit 2
I added the following lines into the findlibusb-1.0.cmake file:
set(LIBUSB_1_LIBRARY C:\\local\\libs\\libusbx\\libusb-1.0.dll)
message(STATUS "***********************************> LIBUSB_1_LIBRARY: ${LIBUSB_1_LIBRARY}")
Here is the output from that:
C:\local\projects\synergy-usb\synergy-through-usb-master>cmake .
-- ***********************************> LIBUSB_1_LIBRARY: C:\local\libs\libusbx\libusb-1.0.dll
-- Found libusb-1.0:
-- - Includes: C:/local/libs/libusbx
-- - Libraries: C:\local\libs\libusbx\libusb-1.0.dll
You have called ADD_LIBRARY for library cryptopp without any source files. This typically indicates a problem with your CMakeLists.txt file
-- Configuring done
CMake Error: CMake can not determine linker language for target: cryptopp
CMake Error: CMake can not determine linker language for target: cryptopp
CMake Error: CMake can not determine linker language for target: cryptopp
CMake Error: CMake can not determine linker language for target: cryptopp
-- Generating done
-- Build files have been written to: C:/local/projects/synergy-usb/synergy-through-usb-master
Still does not build, but this particular library seems to be added ok now :)
synergy had such approach to keep some 3rd party libs compressed in zip in ext subdirectory. synergy-through-usb though kept cmake way to specify include through -DLIBUSB_1_INCLUDE_DIR=... -DLIBUSB_1_LIBRARY=... for Windows.
So you just need go to 'ext' directory and uncompress its zip archives with libraries gtest, gmock and crypto.
I have created a project that uses some boost libraries. Originally the project was created as Eclipse project using MinGW and was working correctly. Now I am trying to convert it into CMake project.
I prepare MinGW makefile with CMake and try to compile it but get the following error:
In file included from D:/Krzych/usr/local/include/boost/date_time/c_time.hpp:17:0,
from D:/Krzych/usr/local/include/boost/date_time/time_clock.hpp:16,
from D:/Krzych/usr/local/include/boost/date_time/posix_time/posix_time_types.hpp:10,
from D:/Krzych/usr/local/include/boost/asio/time_traits.hpp:24,
from D:/Krzych/usr/local/include/boost/asio/deadline_timer_service.hpp:27,
from D:/Krzych/usr/local/include/boost/asio/basic_deadline_timer.hpp:25,
from D:/Krzych/usr/local/include/boost/asio.hpp:22,
from D:/Krzych/Projects/Picard/PicardDebugLink/include/CCommunicationAgent.h:22,
from D:\Krzych\Projects\Picard\PicardDebugLink\source\CCommunicationAgent.cpp:12:
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\ctime:65:11: error: '::difftime' has not been declared
using ::difftime;
^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\ctime:66:11: error: '::mktime' has not been declared
using ::mktime;
The same kind of error is repeated for every symbol from time.h referenced in ctime. As you can see ctime is directly included by boost date_time library.
Edit -- CMake files --
main CMakeLists.txt
cmake_minimum_required (VERSION 2.6)
project (PicardDebugLink)
# The version number.
set (PicardDebugLink_VERSION_MAJOR 0)
set (PicardDebugLink_VERSION_MINOR 9)
if(WIN32)
macro(get_WIN32_WINNT version)
if (WIN32 AND CMAKE_SYSTEM_VERSION)
set(ver ${CMAKE_SYSTEM_VERSION})
string(REPLACE "." "" ver ${ver})
string(REGEX REPLACE "([0-9])" "0\\1" ver ${ver})
set(${version} "0x${ver}" CACHE PATH "x")
endif()
endmacro()
get_WIN32_WINNT(platform_code)
add_definitions(-D_WIN32_WINNT=${platform_code})
endif()
# configure a header file to pass some of the CMake settings
# to the source code
configure_file (
"${PROJECT_SOURCE_DIR}/PicardDebugLinkConfig.h.in"
"${PROJECT_BINARY_DIR}/PicardDebugLinkConfig.h"
)
set(CMAKE_CXX_FLAGS "-std=c++11" CACHE STRING "C++2011 features are required" FORCE)
find_package (Boost REQUIRED atomic chrono date_time system timer thread)
include_directories( ${Boost_INCLUDE_DIRS} )
# add the binary tree to the search path for include files
include_directories("${PROJECT_BINARY_DIR}")
include_directories(include)
add_subdirectory(source)
get_property(inc_dirs DIRECTORY PROPERTY INCLUDE_DIRECTORIES)
message(status "** inc_dirs: ${inc_dirs}")
# build a CPack driven installer package
# include (InstallRequiredSystemLibraries)
set (CPACK_PACKAGE_VERSION_MAJOR "${TreeEight_VERSION_MAJOR}")
set (CPACK_PACKAGE_VERSION_MINOR "${TreeEight_VERSION_MINOR}")
include (CPack)
File for source subdirectory:
# add the executable
add_library(PicardDebugLink SHARED CCommunicationAgent.cpp
CCommProtocol.cpp
CCreateTest.cpp
CDataStorage.cpp
CException.cpp
CFlashErase.cpp
CFlashFileIOBuff.cpp
CFlashMemIOBuff.cpp
CFlashWriteFile.cpp
CFlashWriteMem.cpp
CFrameCreator.cpp
CIOBuff.cpp
CSerialLink.cpp
ErrorCategories.cpp
FlashProtocols.cpp
IThrowable.cpp
ProgramFPGADataHelpers.cpp
ProgramFPGAProtocols.cpp
SPicardCommunicationFrame.cpp
utilities.cpp
)
get_property(inc_dirs DIRECTORY PROPERTY INCLUDE_DIRECTORIES)
target_link_libraries(PicardDebugLink ${Boost_LIBRARIES} )
# add the install targets
install (TARGETS PicardDebugLink DESTINATION bin)
install (FILES "include/*.h" DESTINATION include)