allegro5 project in CLion, ld: library not found error - c++

I'm making a C++ and allegro5 project for university. I compiled allegro library and it's working well in Xcode for example. But I wanted to do my project in CLion and as soon as try to build project including allegro it throws an error:
ld: library not found for -lallegro_acodec
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [TEST1] Error 1
make[1]: *** [CMakeFiles/TEST1.dir/all] Error 2
make: *** [all] Error 2
CMakeLists.txt:
cmake_minimum_required(VERSION 3.3)
project(TEST1)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_executable(TEST1 ${SOURCE_FILES})
INCLUDE_DIRECTORIES( /usr/local/Cellar/allegro/5.0.11/include )
LINK_DIRECTORIES( /usr/local/Cellar/allegro/5.0.11/lib )
TARGET_LINK_LIBRARIES(TEST1
allegro_acodec
allegro_audio
allegro_color
allegro_dialog
allegro_image
allegro_main
allegro_memfile
allegro_physfs
allegro_primitives
allegro_ttf
allegro_font
allegro)
main.cpp:
#include <iostream>
#include <allegro5/allegro.h>
using namespace std;
int main(int argc, char **argv) {
al_init();
return 0;
}
I'm working on OSX 10.11. I could not find solution for my problem. I konw that allegro and CLion are not that popular. Can anyone help me what that error means?

You should issue link_directories before add_executable.
From the documentation about link_directories:
The command will apply only to targets created after it is called.

Related

Compile C++ program with Curl and CMake [MinGW]

I'm trying to compile and execute a simple test program with curl lib.
#include <curl/curl.h>
using namespace std;
int main(){
curl_global_init(CURL_GLOBAL_DEFAULT);
curl_global_cleanup();
return 0;
}
I have a CMakeLists to generate MakeFile :
cmake_minimum_required(VERSION 3.17)
set(CMAKE_CXX_STANDARD 17)
project(ONLY25)
file(GLOB_RECURSE SRCS source/*.cpp)
file(GLOB_RECURSE HDRS source/*.h)
set(EXECUTABLE_NAME "ONLY25")
include_directories("C:/Users/natha/Desktop/work/myLibs/curl-7.71.1-win64-mingw/include")
set(CURL_DIR "C:/Users/natha/Desktop/work/myLibs/curl-7.71.1-win64-mingw")
set(CURL_LIBRARY "C:/Users/natha/Desktop/work/myLibs/curl-7.71.1-win64-mingw/lib/libcurl.dll.a" "C:/Users/natha/Desktop/work/myLibs/curl-7.71.1-win64-mingw/lib/libcurl.a")
set(CURL_INCLUDE_DIR "C:/Users/natha/Desktop/work/myLibs/curl-7.71.1-win64-mingw/include")
find_package(CURL REQUIRED)
add_executable(${EXECUTABLE_NAME} ${SRCS} ${HDRS})
target_link_libraries(${EXECUTABLE_NAME} ${CURL_LIBRARIES})
First, I use :
cmake -G "MinGW Makefiles" .
Everything works well
After that, I use :
C:\MinGW\bin\mingw32-make.exe
And I've got this linking error :
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: CMakeFiles\ONLY25.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x16): undefined reference to `_imp__curl_global_init'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: CMakeFiles\ONLY25.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x1d): undefined reference to `_imp__curl_global_cleanup'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\ONLY25.dir\build.make:106: recipe for target 'ONLY25.exe' failed
mingw32-make[2]: *** [ONLY25.exe] Error 1
CMakeFiles\Makefile2:93: recipe for target 'CMakeFiles/ONLY25.dir/all' failed
mingw32-make[1]: *** [CMakeFiles/ONLY25.dir/all] Error 2
Makefile:101: recipe for target 'all' failed
mingw32-make: *** [all] Error 2
I searched a lot on internet, there are some other posts on this topics but I think I did like they said.
I tried with different version of curl, and I'm using MinGW on Windows 10.
I'm really sorry if it's a dumb mistake, I still have a little trouble with Cmake. I'm stuck here for the whole day...
I'd appreciate your help with some explaination to not reproduce this aberration :)
I tried some new things thanks to responses :
set up FindCURL.cmake by copying this one link
change the CMakeLists
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
message(${CMAKE_MODULE_PATH})
set(CURL_LIBRARY "-lcurl")
find_package(CURL)
message(${CURL_LIBRARIES})
message(${CURL_INCLUDE_DIRS})
include_directories(${CURL_INCLUDE_DIRS})
add_executable(${EXECUTABLE_NAME} ${SRCS} ${HDRS})
target_link_libraries(${EXECUTABLE_NAME} ${CURL_LIBRARIES})
Currently, the cmake command is working well and the variables seems legit:
CURL_LIBRARIES : C:/Users/natha/Desktop/work/myLibs/curl-7.71.1-win64-mingw/lib/libcurl.dll.aC:/Users/natha/Desktop/work/myLibs/curl-7.71.1-win64-mingw/lib/libcurl.a
CURL_INCLUDE_DIRS : C:/Users/natha/Desktop/work/myLibs/curl-7.71.1-win64-mingw/include
However, there is again the linking error with the make command...

CMake running fine but build not working for 3rd party library

I need to use OpenSSL and cpprestsdk in a C++ project I'm working on but I'm having issues getting it to build properly. In my CMakeLists.txt I have:
cmake_minimum_required(VERSION 3.12)
project(Final_Project)
set(CMAKE_CXX_STANDARD 11)
set(OPENSSL_INCLUDE_DIR /usr/local/opt/openssl/bin/openssl)
find_package(cpprestsdk REQUIRED NAMES cpprestsdk cpprest )
find_package(OpenSSL REQUIRED)
add_executable(Final_Project main.cpp)
Which builds just fine and returns no errors. All my code as of right now is contained in main.cpp.
In main.cpp I have:
#include <iostream>
#include <string>
#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
using namespace std;
int main(int argc, char* argv[])
{
cout << "Running!" << endl;
return 0;
}
But whenever I try to build/run it I get an error with cpprest:
Scanning dependencies of target Final_Project
[ 50%] Building CXX object CMakeFiles/Final_Project.dir/main.cpp.o
/Users/myAccount/myFolder/Final Project/main.cpp:3:10: fatal error: 'cpprest/http_client.h' file not found
#include <cpprest/http_client.h>
^~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
make[2]: *** [CMakeFiles/Final_Project.dir/main.cpp.o] Error 1
make[1]: *** [CMakeFiles/Final_Project.dir/all] Error 2
make: *** [all] Error 2
I installed OpenSSL and cpprestsdk with homebrew just fine and I've added everything I think I need to my PATHs. I've tried adding command line arguments to GCC to include the cpprestsdk path and I've tried renaming the #include <cpprest...> to #include <cpprestsdk...> but to no avail. Anyone have any ideas? I'm sure it's just something simple I'm missing.
I ended up figuring it out, my project name had a space in it so I refactored it without that and updated my CMake accordingly. I also had to add the target_link_libraries below the add_executable line. Example of my working CMakeLists below:
cmake_minimum_required(VERSION 3.12)
project(FinalProject)
set(CMAKE_CXX_STANDARD 11)
# Set OpenSSL dir, this *should* be default on linux/mac
set(OPENSSL_INCLUDE_DIR /usr/local/opt/openssl/bin/openssl)
# Get OpenSSL
find_package(OpenSSL REQUIRED)
# Get cppRestSDK
find_package(cpprestsdk REQUIRED)
# Compile + Link
add_executable(FinalProject main.cpp)
target_link_libraries(FinalProject PRIVATE cpprestsdk::cpprest)

Making C++ Project with OpenCV and librealsense

I want to start a computer vision project using an r200 and OpenCV. I'm using the legacy librealsense library and i've ran the examples on it successfully.
Unfortunately i'm new to C++ and Cmake so i'm struggling to import things appropriately.
My project structure looks like:
-Project
-build
-librealsense //this is the directory that contains the code to run the r200
-main.cpp //my main working file
-CMakeLists.txt //what i'm working on
my CMakeLists.txt runs successfully when i execute ../ cmake from the build directory and it looks like this:
cmake_minimum_required(VERSION 3.10)
project(Project)
set(CMAKE_CXX_STANDARD 11)
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_subdirectory(librealsense)
# add the executable
add_executable(Project main.cpp librealsense/examples/example.hpp)
target_link_libraries( Project ${OpenCV_LIBS})
target_link_libraries(Project realsense2)
Note that at the moment i'm just trialing code from the examples and that librealsense/examples/example.hpp is the relative path to one of the example.hpp headers used in the example that i'm copying.
The imports that i'm using in main.cpp are as follows:
#include <librealsense2/rs.hpp>
#include "librealsense/examples/example.hpp"
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
It responds with a very verbose error message that ends with the following:
...
"_rs2_start_processing", referenced from:
void rs2::processing_block::start<rs2::frame_queue>(rs2::frame_queue) in main.cpp.o
"_rs2_stream_to_string", referenced from:
texture::show(rect const&) const in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [Project] Error 1
make[1]: *** [CMakeFiles/Project.dir/all] Error 2
make: *** [all] Error 2
My question is how do I actually import OpenCV and the librealsense libraries!!!
Any direction to good guides for this and CMake would make me forever grateful!

Using CLion, only "main.cpp" being built

I've been working on a project in CLion for quite some time now (over a month). All of a sudden, CLion will not build all of my files... When I run my program, my messages build looks like this:
Scanning dependencies of target main.cpp
[ 11%] Building CXX object CMakeFiles/main.cpp.dir/Custom_Types.cpp.o
[ 22%] Linking CXX executable ../bin/main.cpp
ld: library not found for -l*.cpp
clang: error: linker command failed with exit code 1 (use -v to see
invocation)
make[3]: *** [../bin/main.cpp] Error 1
make[2]: *** [CMakeFiles/main.cpp.dir/all] Error 2
make[1]: *** [CMakeFiles/main.cpp.dir/rule] Error 2
make: *** [main.cpp] Error 2
Great... what the flippin fireworks is going on? This is what my CMake looks like...
cmake_minimum_required(VERSION 3.7)
project(Crazy_Sniper)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bin")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -fpermissive")
find_package (OpenGL REQUIRED)
find_package (GLUT REQUIRED)
include_directories(${OPENGL_INCLUDE_DIR} ${GLUT_INCLUDE_DIRS})
file(GLOB SOURCE_FILES
*.cpp
*.h
)
add_executable(main.cpp ${SOURCE_FILES} Custom_Types.cpp)
target_link_libraries (main.cpp ${OPENGL_LIBRARIES} ${GLUT_LIBRARIES}
mdl pthread)
Does anyone know what the problem is? I've looked through countless forums, and I cannot seem to find someone with a similar problem. Any help would be much appreciated.
Update! I've managed to get CLion to build my files, but main still does nothing... I try simply printing something out, and the console does nothing. I get an error code 11 for some reason...?
Thank you!

GLEW won't work with CMAKE

I'm trying to write a simple program with GLFW and GLEW, and while I could successfully add the needed GLFW libraries I can't do the same with the GLEW ones.
I'm totally new to CMAKE so I don't know what I should do differently. I am using CLion btw. Thanks in advance!
cmake_minimum_required(VERSION 3.3)
project(OpenGLHelloWorld)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_executable(OpenGLHelloWorld ${SOURCE_FILES})
add_subdirectory(../glfw-3.1.2 ${CMAKE_CURRENT_BINARY_DIR}/glfw_bin)
include_directories(../glfw-3.1.2/include)
target_link_libraries(OpenGLHelloWorld glfw ${GLFW_LIBRARIES})
ADD_DEFINITIONS(-DGLEW_STATIC)
include_directories(../glew-1.13.0/include)
link_libraries(../glew-1.13.0/lib)
And the cpp file:
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <stdio.h>
using namespace std;
int main() {
if(!glewInit()) {
fprintf(stderr, "Could not start GLFW3\n");
}
return 0;
}
And the error:
undefined reference to `glewInit#0'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\OpenGLHelloWorld.dir\build.make:97: recipe for target 'OpenGLHelloWorld.exe' failed
mingw32-make.exe[3]: *** [OpenGLHelloWorld.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/OpenGLHelloWorld.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/OpenGLHelloWorld.dir/rule] Error 2
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/OpenGLHelloWorld.dir/all' failed
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/OpenGLHelloWorld.dir/rule' failed
Makefile:161: recipe for target 'OpenGLHelloWorld' failed
mingw32-make.exe: *** [OpenGLHelloWorld] Error 2
You didn't link your compiled code to glew.
target_link_libraries(OpenGLHelloWorld glfw ${GLFW_LIBRARIES} GLEW)
That should fix glew lib linking to your executable.
HTH.