Dynamic link error cmake - c++

When I compile my code using command line everything works fine:
g++ main.cpp -lpngwriter
But when I try using cmake I get undefined reference errors.
CMakeLists.txt:
cmake_minimum_required(VERSION 3.8)
project(myproject)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -lpngwriter")
set(SOURCE_FILES main.cpp)
add_executable(myproject ${SOURCE_FILES})
Any ideas how to fix it?

Consider adding include_directories(/path/to/include) and link_directories(/path/to/lib) before add_executable().
And then insert target_link_libraries(pngwriter) after add_executable().
/path/to shall be replaced with relevant values for your system.

Related

fatal error: 'google/protobuf/port_def.inc' file not found

I am using CLion cmakelist.txt to compile but receive the following error. The program compiled with g++ but somehow lacks dependency when I am using cmake. anyone know how to solve this?
cmake_minimum_required(VERSION 3.19)
project(CTCWordBeamSearch_master)
find_package(PkgConfig)
pkg_check_modules(TensorFlow REQUIRED tensorflow)
link_directories(${TensorFlow_LIBRARY_DIRS})
include_directories(${TensorFlow_INCLUDE_DIRS})
add_compile_definitions(${TensorFlow_CFLAGS_OTHER})
set(CMAKE_CXX_STANDARD 14)
find_package(PythonLibs REQUIRED)
include_directories(${PYTHON_INCLUDE_DIRS})
include_directories(${Protobuf_INCLUDE_DIRS})
include_directories(.)
include_directories(cpp)
include_directories(cpp/proj)
include_directories(cpp/src)
include_directories(cpp/src/pybind)
include_directories(cpp/src/pybind/pybind11)
include_directories(cpp/src/pybind/pybind11/detail)
include_directories(cpp/src/utfcpp)
include_directories(cpp/src/utfcpp/utf8)
include_directories(data)
include_directories(doc)
include_directories(np)
include_directories(py)
include_directories(tf)
add_executable(CTCWordBeamSearch_master
...
)
target_link_libraries(CTCWordBeamSearch_master ${PYTHON_LIBRARIES})
target_link_libraries(CTCWordBeamSearch_master ${TensorFlow_LIBRARIES})

C++ .exe with .dll not running outside of CLion

The program runs just fine when started from inside CLion although it is refusing to run by double clicking the .exe with this error being shown when I try to run it:
The procedure entry point _ZNKSt7__cxx1112basic_stringlcSt11char_traitslcESalcEE12find_last_ofEPKcyy could not be located in the dynamic link library C:\Users\steppers\projects\cyan\bin\libcyan_engine.dll.
It looks to me like an issue with a standard lib or something judging by the basic_string bit but I'm at a complete loss here now.
Here is my root CMakeLists.txt:
cmake_minimum_required(VERSION 3.6)
project(cyan_test)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)
add_subdirectory(engine)
include_directories(engine/include)
set(SOURCE_FILES src/main.cpp)
add_executable(cyan_test ${SOURCE_FILES})
target_link_libraries(cyan_test cyan_engine)
And the engine subdirectory's:
cmake_minimum_required(VERSION 3.6)
project(cyan_engine)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
add_subdirectory(lib/glfw-3.2.1)
include_directories(lib/glfw-3.2.1/include)
add_definitions("-fPIC")
add_subdirectory(lib/g3log-1.2)
include_directories(lib/g3log-1.2/src)
include_directories(include)
set(SOURCE_FILES <src files here>)
add_library(cyan_engine SHARED ${SOURCE_FILES})
target_link_libraries(cyan_engine glfw g3logger)
find_package(OpenGL REQUIRED)
include_directories(${OPENGL_INCLUDE_DIR})
target_link_libraries(cyan_engine ${OPENGL_gl_LIBRARY})
Any help would be greatly appreciated.

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})

CMake "uuid" linking FAIL

when trying to build i get this errors:
undefined reference to 'uuid_generate'
undefined reference to 'uuid_unparse'
this my CMakeLists.txt file:
cmake_minimum_required(VERSION 3.6)
project(Synergy)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -luuid -pthread")
find_package (Threads)
set(SOURCE_FILES Functions.cpp Inotify.cpp Inotify.h main.cpp Master.h Message.cpp Message.h Server.cpp Server.h Threads.cpp )
add_executable(synergy_server ${SOURCE_FILES})
I can solve this problem by creating Makefile by myself and add -luuid flag.
but I want to do it with CMake,I tried to add this flag in the CMakeLists.txt file but it doesn't help :(
I have installed uuid-dev(it's not the Problem )
Hope you can help me.
Have a nice day
edit:
I add target_link_libraries(Synergy uuid) to the end of the file and it works, but there is a better way(look at the answers below)
This is not a good way to write a CMake script. CMake come with a rich set of functionalities that you must use to describe what you want to do.
In your case, you must avoid setting flags directly as you have done. In this way, you will be able to change compiler or even OS and your script will still work.
In your case, I would write something like this:
cmake_minimum_required(VERSION 3.6)
project(Synergy)
set(CMAKE_CXX_STANDARD 11)
# using pkg-config to configure uuid
find_package(PkgConfig REQUIRED)
find_package(Threads REQUIRED)
pkg_search_module(UUID REQUIRED uuid)
set(SOURCE_FILES Functions.cpp Inotify.cpp Inotify.h main.cpp Master.h message.cpp Message.h Server.cpp Server.h Threads.cpp )
add_executable(synergy_server ${SOURCE_FILES})
target_include_directories(synergy_server PUBLIC ${UUID_INCLUDE_DIRS})
target_link_libraries(synergy_server ${UUID_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
By the way, what means uuid-dev? I have no such thing in my system

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})