I'm attempting to alter my CmakeLists.txt file for my currently working MacOSX version of my OpenGL engine, so that it will compile and run on windows. I'm positive that I'm doing something wrong in the way I'm writing my Cmakelists.txt and would like some help correcting it.
Have already tried a multitude of different tutorials and examples found online. The problem most likely lies in how I'm trying to link dll files but I'm unsure of what I'm doing wrong.
cmake_minimum_required(VERSION 3.1)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -w")
project (sake)
add_subdirectory(src/imgui)
add_subdirectory(libs/assimp)
add_subdirectory(src/GLM/glm)
include_directories(${PROJECT_SOURCE_DIR})
file(GLOB SRC_FILES src/*.cpp)
add_executable(sake ${SRC_FILES})
target_link_libraries(sake imgui)
target_link_libraries(sake assimp)
# glfw
if (WIN32)
add_subdirectory(F:/glfw-3.3 F:/glfw-3.3/src)
include_directories(F:/glfw-3.3/include)
link_libraries(${GLFW_LIBRARY_DIRS})
endif (WIN32)
if(APPLE)
find_package(glfw3 REQUIRED)
include_directories(${GLFW_INCLUDE_DIRS})
link_libraries(${GLFW_LIBRARY_DIRS})
endif (APPLE)
# opengl
find_package(OpenGL REQUIRED)
include_directories(${OPENGL_INCLUDE_DIRS})
# glew
if(WIN32)
include_directories(F:/GLEW/glew-2.1.0/include)
endif (WIN32)
if(APPLE)
find_package(GLEW REQUIRED)
include_directories(${GLEW_INCLUDE_DIRS})
endif (APPLE)
if(WIN32)
include_directories(libs/assimp/include)
endif(WIN32)
if(APPLE)
find_package(assimp REQUIRED)
include_directories(${ASSIMP_INCLUDE_DIR})
endif(APPLE)
if (APPLE)
find_library(COCOA_LIBRARY Cocoa)
find_library(OpenGL_LIBRARY OpenGL)
find_library(IOKIT_LIBRARY IOKit)
find_library(COREVIDEO_LIBRARY CoreVideo)
SET(EXTRA_LIBS ${COCOA_LIBRARY} ${OpenGL_LIBRARY} ${IOKIT_LIBRARY} ${COREVIDEO_LIBRARY})
endif (APPLE)
target_link_libraries(
sake
glfw
${OPENGL_LIBRARIES}
${GLEW_LIBRARIES}
${EXTRA_LIBS}
)
add_custom_target(run
COMMAND ./sake
DEPENDS sake
WORKING_DIRECTORY ${CMAKE_PROJECT_DIR}
)
current error message happens when I cmake .
CMake Error at F:/glfw-3.3/src/CMakeLists.txt:95 (add_library):
add_library cannot create target "glfw" because another target with the
same name already exists. The existing target is a shared library created
in source directory "F:/glfw-3.3/src". See documentation for policy
CMP0002 for more details.
Related
I'm developing a cpp project in Clion. For the build I use cmake. Last weekend I upgraded from ubuntu 18.04 to 20.04, from then I cannot build my project and I get the error:
/usr/local/include/boost/system/error_code.hpp:221: undefined reference to `boost::system::generic_category()'. It seems that something in the linking isn't right. I reinstalled all packages, tried different versions but nothing. My CMakeLists.txt is:
cmake_minimum_required(VERSION 3.7)
project(pki_cpp)
set(CMAKE_CXX_STANDARD 11)
set(THREADS_PREFER_PTHREAD_FLAG ON)
#find_package(libmongocxx REQUIRED)
find_package(mongocxx REQUIRED)
find_package(bsoncxx REQUIRED)
include_directories(${LIBMONGOCXX_INCLUDE_DIR})
include_directories(${LIBBSONCXX_INCLUDE_DIR})
include_directories(SYSTEM ./lib)
set(BOOST_ROOT /usr/lib/)
find_package(Boost 1.71.0 REQUIRED COMPONENTS system chrono thread filesystem)
include_directories(${Boost_INCLUDE_DIR})
if(NOT Boost_FOUND)
message(FATAL_ERROR "Could not find boost!")
endif()
find_package(Threads REQUIRED)
find_package(OpenSSL REQUIRED)
# A custom library
add_subdirectory(asnDatatypes)
include_directories({CMAKE_CURRENT_SOURCE_DIR} asnDatatypes)
set_property(TARGET asnLibrary PROPERTY POSITION_INDEPENDENT_CODE ON)
# end custom library
#cryptopp library
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/contrib/cmake")
find_package(CryptoPP REQUIRED)
include_directories(${CRYPTOPP_INCLUDE_DIR})
link_directories(${CryptoPP_INCLUDE_DIR})
# end cryptopp
add_executable(pki_cpp main.cpp rootCA.cpp rootCA.h)
target_include_directories(pki_cpp PRIVATE ${Boost_INCLUDE_DIRS} ${LIBMONGOCXX_INCLUDE_DIRS} )
link_directories(pki_cpp ${Boost_LIBRARY_DIRS})
target_link_libraries(pki_cpp ${Boost_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${Boost_FILESYSTEM_LIBRARY} ${Boost_SYSTEM_LIBRARY} ${Boost_LIBRARIES} Boost::system Threads::Threads boost_system asnLibrary OpenSSL::SSL ${CRYPTOPP_LIBRARIES} ${LIBMONGOCXX_LIBRARIES} mongo::bsoncxx_shared mongo::mongocxx_shared ${CMAKE_THREAD_LIBS_INIT} ${Boost_FILESYSTEM_LIBRARY} ${Boost_SYSTEM_LIBRARY} Boost::system)
Any help is appreciated!
That's a lot of stuff, all randomly ordered and commented out. The obvious error is a failure to link Boost System
I'd go with
FIND_PACKAGE(Boost 1.65.0
COMPONENTS system program_options REQUIRED)
LINK_DIRECTORIES(${Boost_LIBRARY_DIRS})
INCLUDE_DIRECTORIES(${Boost_INLUDE_DIRS})
LINK_LIBRARIES(${Boost_LIBRARIES})
Alternatively you can be specific and link specific libs to specific targets
TARGET_LINK_LIBRARIES(mylib ${Boost_SYSTEM_LIBRARY})
TARGET_LINK_LIBRARIES(myexecutable ${Boost_THREAD_LIBRARY})
I'm trying to learn OpenGL from a tutorial, and I need to link SOIL2 to my project.
I tried to write my CMake like this:(all other libs are working, Cmake gets no error but I can't include SOIL2.h because of 'SOIL2.h' file not found). I'm working with Clion IDE.
cmake_minimum_required(VERSION 3.15)
project(OpenGL_Tutorial)
set(CMAKE_CXX_STANDARD 14)
add_executable(OpenGL_Tutorial main.cpp)
find_package(OpenGL REQUIRED)
find_package(SDL2 REQUIRED)
find_package(PkgConfig REQUIRED)
pkg_search_module(GLFW REQUIRED glfw3)
find_library(SOIL2 lib/libsoil2.a)
include_directories(
${OPENGL_INCLUDE_DIRS}
${GLEW_INCLUDE_DIRS}
${GLFW_INCLUDE_DIRS}
${SDL2_INCLUDE_DIR}
)
target_link_libraries(OpenGL_Tutorial
${OPENGL_LIBRARY}
${GLEW_LIBRARY}
${GLFW_LIBRARIES}
${SDL2_LIBRARY}
${SOIL2_LIBRARY}
)
libsoil2.a is located in my lib directory in the project directory
(see project direcory image)
Thanks!
I am modifying a C++ application to demonstrate ambient occlusion based on OpenGL, GLFW and GLAD libraries. I would like to use AntTweakBar library as well, but I don't know how to modify cMakeLists.txt to properly import it. I have tried a lot of different versions to import this library, which is in the root of the project anyway.
Below you can see my cmakelists.txt.
cmake_minimum_required(VERSION 2.8)
cmake_policy(VERSION 2.8)
project(LearnOpenGL)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/modules/")
link_directories(${CMAKE_SOURCE_DIR}/lib)
list(APPEND CMAKE_CXX_FLAGS "-std=c++11")
# find the required packages
find_package(GLM REQUIRED)
message(STATUS "GLM included at ${GLM_INCLUDE_DIR}")
find_package(GLFW3 REQUIRED)
message(STATUS "Found GLFW3 in ${GLFW3_INCLUDE_DIR}")
find_package(ASSIMP REQUIRED)
message(STATUS "Found ASSIMP in ${ASSIMP_INCLUDE_DIR}")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall")
find_package(OpenGL REQUIRED)
add_definitions(${OPENGL_DEFINITIONS})
find_package(X11 REQUIRED)
# note that the order is important for setting the libs
# use pkg-config --libs $(pkg-config --print-requires --print-requires-private glfw3) in a terminal to confirm
set(LIBS ${GLFW3_LIBRARY} X11 Xrandr Xinerama Xi Xxf86vm Xcursor GL dl pthread ${ASSIMP_LIBRARY})
set(CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_CXX_LINK_EXECUTABLE} -ldl")
configure_file(configuration/root_directory.h.in configuration/root_directory.h)
include_directories(${CMAKE_BINARY_DIR}/configuration)
# first create relevant static libraries requried for other projects
add_library(GLAD "src/glad.c")
set(LIBS ${LIBS} GLAD)
FIND_PATH(ANT_TWEAK_BAR_INCLUDE_PATH AntTweakBar.h
PATHS
${CMAKE_BINARY_DIR}/AntTweakBar/include)
FIND_LIBRARY( ANT_TWEAK_BAR_LIBRARY AntTweakBar
PATHS
${CMAKE_BINARY_DIR}/AntTweakBar/lib
)
#reate a project file
file(GLOB SOURCE
"src/*.h"
"src/*.cpp"
"src/*.vs"
"src/*.fs"
"src/*.gs"
)
set(NAME "SSAO")
add_executable(${NAME} ${SOURCE})
target_link_libraries(${NAME} ${LIBS} ${ANT_TWEAK_BAR_LIBRARY})
set_target_properties(${NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin/")
include_directories(${CMAKE_SOURCE_DIR}/includes)
include_directories(${ANT_TWEAK_BAR_INCLUDE_PATH})
This is an example how i import static googletest libraries 'libgtest.a' and 'libgmock.a' to a project using cmake as build configuration generator.
For shared libraries you need to replace STATIC by SHARED in add_library.
cmake_minimum_required(VERSION 2.8)
project(gtest_sample1)
include_directories(../googletest/include)
include_directories(../googlemock/include)
add_library(gtest STATIC IMPORTED)
set_property(TARGET gtest PROPERTY IMPORTED_LOCATION /home/mschmid/projects/googletest-master/gtest/libgtest.a)
add_library(gmock STATIC IMPORTED)
set_property(TARGET gmock PROPERTY IMPORTED_LOCATION /home/mschmid/projects/googletest-master/gmock/libgmock.a)
add_executable(gtest_sample1 main.cpp)
target_link_libraries(gtest_sample1 gtest gmock pthread)
My project structure looks like this:
CMakeLists.txt
deps
glew
glfw
include
src
...
graphics
...
CMakeLists.txt
Two CMakeLists.txt files that deserve attention:
CMakeLists.txt
cmake_minimum_required(VERSION 3.9)
project(noam_engine)
find_package(glfw3 REQUIRED)
find_package(OpenGL REQUIRED)
find_package(GLEW REQUIRED)
set(CMAKE_CXX_STANDARD 11)
set(NE_LIBRARIES common math graphics)
FOREACH(lib ${NE_LIBRARIES})
add_subdirectory(src/${lib})
ENDFOREACH(lib)
add_executable(noam_engine src/main.cpp)
if(OPENGL_FOUND AND GLEW_FOUND)
target_include_directories(noam_engine PUBLIC include ${OPENGL_INCLUDE_DIR} ${GLEW_INCLUDE_DIRS})
target_link_libraries(noam_engine ${NE_LIBRARIES})
endif()
src/graphics/CMakeLists.txt
cmake_minimum_required(VERSION 3.9)
find_package(glfw3 REQUIRED)
find_package(OpenGL REQUIRED)
find_package(GLEW REQUIRED)
file(GLOB SRC "*.cpp")
add_library(graphics ${SRC})
if(OPENGL_FOUND AND GLEW_FOUND)
target_include_directories(graphics PUBLIC ${PROJECT_SOURCE_DIR}/include ${OPENGL_INCLUDE_DIR} ${GLEW_INCLUDE_DIRS})
target_link_libraries(graphics ${OPENGL_gl_LIBRARY} ${GLFW3_LIBRARY} ${GLEW_LIBRARIES})
message(STATUS "GLFW and GLEW successfully linked")
message(STATUS ${OPENGL_gl_LIBRARY})
message(STATUS ${GLFW3_LIBRARY})
message(STATUS ${GLEW_LIBRARIES})
else()
message(STATUS "Cannot find GL libraries")
endif()
In a few words I want to create a bunch of static libraries of the engine, in particular, link graphics library with GL's, and finally link all of them with the executable in a root CMakeLists.txt.
But I noticed that ${GLFW3_LIBRARY} is empty and I got a linker error, for example, when I call glfwInit(). I followed the guide during building and installation of GLFW
cmake .
make
make install
I believe headers and libraries are in /usr/local/*, but apparently CMake cannot find them or maybe I did something wrong.
The only hypothesis I have is that find_package doesn't know about glfw3Config.cmake which is in deps/glfw/*
I took the script from https://github.com/JoeyDeVries/LearnOpenGL/blob/master/cmake/modules/FindGLFW3.cmake and put it into cmake folder. Then in CMakeLists.txt I added
list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
Now everything works properly
Thanks #Tsyvarev
I am porting a C++ project I have been working on for 2 years, to Windows, and I am facing this link issue, that I haven't seen on any google-referenced posts... :/
It is a CMake-generated project, and I get these CMake entries set by default to "\machine:x64"
CMAKE_EXE_LINKER_FLAGS
CMAKE_MODULE_LINKER_FLAGS
CMAKE_SHARED_LINKER_FLAGS
CMAKE_STATIC_LINKER_FLAGS
I tried removing the flag, which has no effect on any of the 3 last entries, but CMAKE_EXE_LINKER_FLAGS without this value gives me about 85 unresolved externals spread in my libraries.
ALL my projects / project dependencies are compiled with MSVC2015 in x64.
Linking fails both in debug & release mode (not that it matters..)
I should also probably mention that I have 6 projects in this solution, 4 libraries, 2 executables. 4 of these projects are compiling fine (1 executable & 4 libs), only the 2 last ones are failing (with the same link error for both).
The only difference in terms of dependencies are a dependency to the Point Cloud Library (pcl), and to Qt5 (Core GUI, XML, and OpenGL)
Did anyone already encountered this specific linker error?
Hope you can help =)
EDIT: Here are the 2 main CMakelists files (the one at the root of the project and the one that compiles both failing projects). I removed everything that didn't seem pertinent for this issue.
Root CMakeLists.txt:
project(SofaOR)
cmake_minimum_required(VERSION 2.4)
if (COMMAND cmake_policy)
cmake_policy(SET CMP0003 NEW)
endif(COMMAND cmake_policy)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
include_directories(${CMAKE_SOURCE_DIR})
#Reading local file for the non automatically detected dependencies
if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/cmake/dependencies.cmake")
message("Adding custom file")
include(${CMAKE_CURRENT_LIST_DIR}/cmake/dependencies.cmake)
endif()
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
if(COMPILER_SUPPORTS_CXX11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
elseif(COMPILER_SUPPORTS_CXX0X)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
else()
message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
endif()
set(SOFA_ALL_LIBS SofaHelper SofaDefaultType)
set(SOFA_INCLUDE_DIRS ${SOFA_DIR}/include)
set(SOFA_LIBRARY_DIRS ${SOFA_DIR}/lib)
add_subdirectory(${CMAKE_SOURCE_DIR}/processOR)
the CMakelists included by the last add_subdirectory:
project(processOR)
cmake_minimum_required(VERSION 3.0.2)
if (COMMAND cmake_policy)
cmake_policy(SET CMP0003 NEW)
endif(COMMAND cmake_policy)
set(PROCESSOR_EXEC_NAME processOR)
set(PROCESSOR_LIB_NAME processOR_lib)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
find_package(OpenCV COMPONENTS core imgproc xfeatures2d calib3d REQUIRED )
find_package(Qt5 COMPONENTS Core Gui Xml OpenGL REQUIRED )
find_package(PCL COMPONENTS common io kdtree REQUIRED )
find_package(Boost COMPONENTS program_options chrono thread REQUIRED)
find_package(OpenIGTLink REQUIRED )
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(PROCESSOR_INCLUDES ${CMAKE_CURRENT_SOURCE_DIR}/src/ PARENT_SCOPE)
set(PROCESSOR_SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/)
include_directories(${PCL_INCLUDE_DIRS})
include_directories(${COMMON_INCLUDE_DIRS})
include_directories(${OpenIGTLink_INCLUDE_DIRS})
include_directories(${OpenCV_INCLUDE_DIRS})
include_directories(${QGLVIEWER_DIR})
include_directories(${Boost_INCLUDE_DIRS})
include_directories(${SOFA_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
link_directories(${COMMON_LIBRARY_DIRS})
link_directories(${OpenIGTLink_LIBRARY_DIRS})
link_directories(${OpenCV_LIBRARY_DIRS})
link_directories(${QGLVIEWER_DIR})
link_directories(${SOFA_LIBRARY_DIRS})
link_directories(${Boost_LIBRARY_DIRS})
include_directories(${PROCESSOR_SRC})
set(PROCESSOR_ALL_LIBS
${PCL_LIBRARIES}
${COMMON_LIBRARIES}
OpenIGTLink
${OpenCV_LIBS}
QGLViewer
${Boost_LIBRARIES}
${SOFA_ALL_LIBS}
)
set (HEADER_FILES
${PROCESSOR_SRC}/util/Calibration.h
${PROCESSOR_SRC}/fetchers/VideoFetcher.h
[…]
${PROCESSOR_SRC}/filters/PointCloudTriangulator.h
${PROCESSOR_SRC}/filters/PointCloudSmootherMLS.h
${PROCESSOR_SRC}/filters/PointCloudSegmenter.h
${PROCESSOR_SRC}/filters/PointCloudDownSampler.h
)
set (SOURCE_FILES
${PROCESSOR_SRC}/Registrator.cpp
${PROCESSOR_SRC}/tinyxml2.cpp
[…]
${PROCESSOR_SRC}/filters/PointCloudSegmenter.cpp
${PROCESSOR_SRC}/filters/PointCloudDownSampler.cpp
)
# Library
add_library(${PROCESSOR_LIB_NAME} SHARED ${HEADER_FILES} ${SOURCE_FILES})
target_link_libraries(${PROCESSOR_LIB_NAME} ${PROCESSOR_ALL_LIBS} ${CMAKE_EXE_LINKER_FLAGS} ${ADDITIONAL_LIBRARIES})
QT5_USE_MODULES(${PROCESSOR_LIB_NAME} Widgets Gui OpenGL Xml)
set(PROCESSOR_LIB ${PROCESSOR_LIB_NAME} PARENT_SCOPE)
# Main executable
set (SOURCE_FILES
${PROCESSOR_SRC}/mainRegistration.cpp
)
add_executable(${PROCESSOR_EXEC_NAME} ${HEADER_FILES} ${SOURCE_FILES})
target_link_libraries(${PROCESSOR_EXEC_NAME} ${PROCESSOR_LIB} ${Boost_LIBRARIES} ${PROCESSOR_LIB_NAME} ${CMAKE_EXE_LINKER_FLAGS})
QT5_USE_MODULES(${PROCESSOR_EXEC_NAME} Widgets Gui OpenGL Xml)
set_target_properties(${PROJECT_NAME}
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
)