C++: static link not found - c++

I make a libtest_lib.a file with cmake.
cmake_minimum_required(VERSION 3.8)
project(test)
set(CMAKE_CXX_STANDARD 98)
set(SOURCE_FILES library.cpp library.h)
add_library(test_lib ${SOURCE_FILES})
then in my executable C++ project ,I include the #include "library.h"
and the CMakeList.txt:
cmake_minimum_required(VERSION 3.8)
project(study)
set(CMAKE_CXX_STANDARD 98)
set(SOURCE_FILES main.cpp)
add_executable(study ${SOURCE_FILES})
target_link_libraries(study libtest_lib.a) //libtest_lib.a file under the project path
but it fails.
/Users/bin381/CLionProjects/study/main.cpp:1:10: fatal error: 'library.h' file not found

Please add the include directories as mentioned in the documentation : https://cmake.org/cmake/help/v3.0/command/include_directories.html
Otherwise cmake will not be able to find where to look in for include files.

Related

Creating c++ library using cmake

Dear cmake / c++ experts,
I created a small library for display and input on the console. Now I would like to use it in another project. Unfortunately, even after quite some time spent in the cmake documents, I cannot get it to work.
The library is here: https://github.com/HEIGVD-PRG1-F-2022/prg1f-io and uses the following CMakeLists.txt:
cmake_minimum_required(VERSION 3.23)
project(prg1f-io VERSION 0.1 DESCRIPTION "Input and display methods for PRG1-F")
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
add_library(prg1f-io STATIC src/display.cpp)
set_target_properties(prg1f-io PROPERTIES VERSION ${PROJECT_VERSION})
set_target_properties(prg1f-io PROPERTIES PUBLIC_HEADER include/display.h)
include(GNUInstallDirs)
install(TARGETS prg1f-io
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
And I created a small test here: https://github.com/HEIGVD-PRG1-F-2022/prg1f-test with the following CMakeLists.txt:
cmake_minimum_required(VERSION 3.23)
project(prg1f_test)
set(CMAKE_CXX_STANDARD 20)
include(FetchContent)
FetchContent_Declare(prg1f-io
GIT_REPOSITORY https://github.com/HEIGVD-PRG1-F-2022/prg1f-io.git
GIT_TAG main
)
FetchContent_MakeAvailable(prg1f-io)
add_executable(prg1f-test main.cpp)
target_link_libraries(prg1f-test PRIVATE prg1f-io)
While the CMakeLists.txt in the test does download the library and compiles it, I cannot get access to the header files in the test. I tried different includes, but none of the following work:
#include <prg1f-io/include/display.h>
#include <prg1f-io/display.h>
#include <prg1f-io>
#include <display.h>
I'm sure it's a little line somewhere in one of the CMakeLists.txt, but I cannot find it.
Any help is greatly appreciated!
Thanks to #Tsyvarev, I can now rest in peace:
The CMakeLists.txt of the library:
cmake_minimum_required(VERSION 3.23)
project(prg1f-io VERSION 0.1 DESCRIPTION "Input and display methods for PRG1-F")
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
add_library(prg1f-io STATIC src/display.cpp)
set_target_properties(prg1f-io PROPERTIES VERSION ${PROJECT_VERSION})
target_include_directories(prg1f-io PUBLIC include)
And the CMakeLists.txt of the binary using the library:
cmake_minimum_required(VERSION 3.23)
project(prg1f_test)
set(CMAKE_CXX_STANDARD 20)
include(FetchContent)
FetchContent_Declare(prg1f-io
GIT_REPOSITORY https://github.com/HEIGVD-PRG1-F-2022/prg1f-io.git
GIT_TAG main
)
FetchContent_MakeAvailable(prg1f-io)
add_executable(prg1f-test main.cpp)
target_link_libraries(prg1f-test PRIVATE prg1f-io)
Now I can include the headers using:
#include <display.h>
All I'm missing now is how to #include<prg1f-io/display.h> without having to add a prg1f-io/include/prg1f-io/display.h directory.

CLion/CMake can find SDL in one project, but not another

I have two C++ projects in SDL, both using SDL. For whatever reason, one of these projects can find SDL just fine. For the other, I can write code as if SDL was found- with autocompletion and everything- but when it comes to building the project, I am informed that fatal error: 'SDL2/SDL.h' file not found.
The CMakeLists.txt files of both projects are basically identical.
CMakeLists.txt for the project that doesn't work:
cmake_minimum_required(VERSION 3.12)
project(Snake)
set(CMAKE_CXX_STANDARD 11)
include_directories(.)
find_package(SDL2 REQUIRED)
add_executable(${PROJECT_NAME}
main.cpp
SnakeBlock.cpp
SnakeBlock.h
SnakeGame.cpp
SnakeGame.h
SnakeBoard.cpp
SnakeBoard.h)
target_link_libraries(${PROJECT_NAME} ${SDL2_LIBRARY})
And CMakeLists.txt for the project that does work:
cmake_minimum_required(VERSION 3.12)
project(Tetris)
set(CMAKE_CXX_STANDARD 11)
include_directories(.)
find_package(SDL2 REQUIRED)
add_executable(${PROJECT_NAME}
Game.cpp
Game.hpp
main.cpp
Playfield.cpp
Playfield.hpp
Tetromino.cpp
Tetromino.hpp)
target_link_libraries(${PROJECT_NAME} ${SDL2_LIBRARY})
And for both projects, SDL2 appears under the "External Libraries" section on the right sidebar, under Header Search Paths.
What gives?

How to add allegro Library in Clion and CMake?

I am trying to compile my game project using Clion IDE but I have a problem when porting allegro 5. I get this error:
main.cpp:2:10: fatal error: 'allegro/allegro.h' file not found
#include <allegro/allegro.h>
My CMakeLists is:
cmake_minimum_required(VERSION 3.5)
project(testAllegro)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_executable(testAllegro ${SOURCE_FILES})
INCLUDE_DIRECTORIES( /usr/local/include )
LINK_DIRECTORIES( /usr/local/lib )
file(GLOB LIBRARIES "/usr/local/Cellar/allegro/5.2.1.1_1/lib/*.dylib")
message("LIBRARIES = ${LIBRARIES}")
TARGET_LINK_LIBRARIES(testAllegro ${LIBRARIES})
Just I want to ask how can I add external Library allegro to Clion?
when you install allegro using homebrew link
use this cmake to compile clion project
cmake_minimum_required(VERSION 3.5)
project(testAllegro)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_executable(testAllegro ${SOURCE_FILES})
INCLUDE_DIRECTORIES( /usr/local/Cellar/allegro/5.2.1.1_1/include )
LINK_DIRECTORIES( /usr/local/Cellar/allegro/5.2.1.1_1/lib )
file(GLOB LIBRARIES "/usr/local/Cellar/allegro/5.2.1.1_1/lib/*.dylib")
message("LIBRARIES = ${LIBRARIES}")
TARGET_LINK_LIBRARIES(testAllegro ${LIBRARIES})

Building wxWidgets 3.1.0 on CLion (Ubuntu)

I am currently trying to build wxWidgets-3.1.0 on a CLion 1.3 project. I use Ubuntu 16.04 (64 bit). Basically, I edited the CMakeLists.txt file like this:
cmake_minimum_required(VERSION 3.5)
project(WxProva)
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake_modules"
${CMAKE_MODULE_PATH})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_executable(WxProva ${SOURCE_FILES})
find_package(wxWidgets)
include_directories(${wxWidgets_INCLUDE_DIRS})
target_link_libraries(WxProva ${wxWidgets_LIBRARIES})
The "External Libraries" section also shows me wxWidgets, but when it comes to write some lines on my main.cpp, everything related with the library seems to be unreachable by the compiler (it's all written in red, like an error). Anyway, if I try to compile, that's the result:
/home/federico/ClionProjects/WxProva/main.cpp:2:35: fatal error: wxWidgets-3.1.0/include: File o directory non esistente
compilation terminated.
Which is like "File or directory doesn't exists."
How can I fix this?
After some experiments here solution. You can just copy it and change some information and ready to build and run.
cmake_minimum_required(VERSION 3.7)
project(Your_Project_Name) //any name for your project
set(CMAKE_CXX_STANDARD 11)
set(wxWidgets_ROOT_DIR </usr/include/wx-3.0-unofficial>) // here I am giving where to search for wxwidgets library. it can be different for you
set(wxWidgets_CONFIGURATION mswu)
find_package(wxWidgets COMPONENTS core base REQUIRED)
include(${wxWidgets_USE_FILE})
set(SOURCE_FILES main.cpp)
add_executable(FirstC ${SOURCE_FILES})
target_link_libraries(FirstC ${wxWidgets_LIBRARIES})
For more Information read https://wiki.wxwidgets.org/CMake
Edit 1
Here you shouldn't even add some compile and link config (wx-config --cxxflags and wx-config --libs) as it is necessary in NetBeans
Here is example configuration for macOS 10.14.4 (Mojave) and CLion 2019.1
(/usr/local is folder where I installed wxWidgets)
cmake_minimum_required(VERSION 3.14)
project(wx1Test)
set(CMAKE_CXX_STANDARD 14)
set(wxWidgets_ROOT_DIR </usr/local/include/wx-3.1>)
set(wxWidgets_CONFIGURATION mswu)
find_package(wxWidgets COMPONENTS core base REQUIRED)
include(${wxWidgets_USE_FILE})
set(SOURCE_FILES main.cpp)
add_executable(wx1Test ${SOURCE_FILES})
target_link_libraries(wx1Test ${wxWidgets_LIBRARIES})

Unable to locate SimpleAmqpClient library for the C++ project on OSX

I try to use the SimpleAmqpClient library to build my multi-agent environment for simulation. I have installed the library after cloning its sources, making them:
make
sudo make install
After that, I created the
main.cpp
file:
#include <iostream>
#include <SimpleAmqpClient/SimpleAmqpClient.h>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
}
just to try it out.
Also, I have the following
CMakeLists.txt:
cmake_minimum_required(VERSION 3.3)
project(SampleProject)
include_directories('/usr/local/include/')
find_package(libSimpleAmqpClient REQUIRED)
include_directories(${libSimpleAmqpClient++_INCLUDE_DIRS})
set(LIBS ${LIBS} ${libSimpleAmqpClient++_LIBRARIES})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_executable(SampleProject ${SOURCE_FILES})
So, the question is: how to find and link this library.
You need to include the line
target_link_libraries(SampleProject ${LIBS})
after the line
add_executable(SampleProject ${SOURCE_FILES})
This imbues the target SampleProject with properties that tell the generator to generate a linker command which reference the libraries you need.
I have found the solution, thanks to #RichardHodges. The solution was to use
find_library() instead of find_package().
THe final file is the following:
CMakeLists.txt
cmake_minimum_required(VERSION 3.3)
project(SampleProject)
include_directories('/usr/local/include/')
find_library(libSimpleAmqpClient REQUIRED)
include_directories(${libSimpleAmqpClient++_INCLUDE_DIRS})
set(LIBS ${LIBS} ${libSimpleAmqpClient++_LIBRARIES})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_executable(SampleProject ${SOURCE_FILES})