I'm trying use Allegro5 in CLion but I can't references addons althought it can find header and lib files.
I have the library in "/usr/lib" and headers files in "/usr/include", besides, I have the library and headers in the proyect directory.
I can use and compile this:
[..]
al_set_window_title(display, "title");
al_clear_to_color(al_map_rgb(4,0,90));
ALLEGRO_COLOR electric_blue = al_map_rgb(44, 117, 255);
But I can't compile this (in spite of detect header and the funcion):
#include <allegro5/allegro_primitives.h>
[..]
al_draw_line(100,500,300,500,electric_blue,6.0);
This is the error:
Scanning dependencies of target project
[ 9%] Building CXX object CMakeFiles/project.dir/main.cpp.o
[ 18%] Linking CXX executable project
CMakeFiles/project.dir/main.cpp.o: En la función `main':
/home/lifka/Desktop/tetris/project/main.cpp:80: reference to `al_draw_line' undefined
collect2: error: ld devolvió el estado de salida 1
make[2]: *** [CMakeFiles/project.dir/build.make:329: project] Error 1
make[1]: *** [CMakeFiles/Makefile2:68: CMakeFiles/project.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
It's the CMakeList.txt:
cmake_minimum_required(VERSION 3.7)
project(project)
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES main.cpp [..])
SET(ALLEGRO_ROOT allegro/)
INCLUDE_DIRECTORIES( ${ALLEGRO_ROOT}/include)
LINK_DIRECTORIES( /${ALLEGRO_ROOT}/lib )
add_executable(project ${SOURCE_FILES})
TARGET_INCLUDE_DIRECTORIES(project PUBLIC ${ALLEGRO_ROOT})
TARGET_LINK_LIBRARIES(project allegro)
This is the project structure:
What am I doing wrong?
As you can see in your error, the reference to 'al_draw_line' in your main.cpp:80 is undefined. This is because Allegro has so many addons that you also have to link if you are using them to make it work.
To solve it, replace
TARGET_LINK_LIBRARIES(project allegro)
by
TARGET_LINK_LIBRARIES(project
allegro_acodec
allegro_audio
allegro_color
allegro_dialog
allegro_image
allegro_main
allegro_memfile
allegro_physfs
allegro_primitives
allegro_ttf
allegro_font
allegro)
Or just make the list with any addon you need. In this case, for example, for using 'al_draw_line' you only need 'allegro_primitives' to make it works.
Related
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...
I have been trying to implement the runtime library which is a header-only library of CodeSynthesis. But I only get a linking compiling error whenever I try to run the generated files which are made by the XSD executable.
Here is the error to show you that I have a linking problem with the runtime library:
[100%] Linking CXX executable XercesGebeuren.exe
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\XercesGebeuren.dir/objects.a(hello.cxx.obj): in function `xsd::cxx::xml::initialize()':
C:/Users/husey/Desktop/XercesGebeuren/xsd-4.0.0-i686-windows/libxsd/xsd/cxx/xml/elements.hxx:84: undefined reference to `xercesc_3_2::XMLPlatformUtils::Initialize(char const*, char const*, xercesc_3_2::PanicHandler*, xercesc_3_2::MemoryManager*)'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\XercesGebeuren.dir/objects.a(hello.cxx.obj): in function `xsd::cxx::xml::terminate()':
C:/Users/husey/Desktop/XercesGebeuren/xsd-4.0.0-i686-windows/libxsd/xsd/cxx/xml/elements.hxx:90: undefined reference to `xercesc_3_2::XMLPlatformUtils::Terminate()'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\XercesGebeuren.dir/objects.a(hello.cxx.obj): in function `xsd::cxx::xml::sax::std_input_stream::std_input_stream(std::istream&)':
C:/Users/husey/Desktop/XercesGebeuren/xsd-4.0.0-i686-windows/libxsd/xsd/cxx/xml/sax/std-input-source.hxx:27: undefined reference to `xercesc_3_2::BinInputStream::BinInputStream()'
---- [And so on] ----
collect2.exe: error: ld returned 1 exit status
mingw32-make[3]: *** [CMakeFiles\XercesGebeuren.dir\build.make:122: XercesGebeuren.exe] Error 1
mingw32-make[2]: *** [CMakeFiles\Makefile2:95: CMakeFiles/XercesGebeuren.dir/all] Error 2
mingw32-make[1]: *** [CMakeFiles\Makefile2:102: CMakeFiles/XercesGebeuren.dir/rule] Error 2
mingw32-make: *** [Makefile:137: XercesGebeuren] Error 2
I have followed the guide given by CodeSynthesis to generate my hello.cxx & hello.hxx files with the command: xsd cxx-tree --std c++11 hello.xsd
CMake
cmake_minimum_required(VERSION 3.17)
project(XercesGebeuren)
set(CMAKE_CXX_STANDARD 20)
set(Xsd_DIR ./cmake)
find_package(XercesC REQUIRED)
find_package(Xsd REQUIRED)
add_executable(XercesGebeuren main.cpp src/hello.cxx)
add_library(mylib INTERFACE)
target_include_directories(mylib PUBLIC INTERFACE "./libxsd/")
target_include_directories(XercesGebeuren PUBLIC ${XSD_INCLUDE_DIR})
target_link_libraries(XercesGebeuren PUBLIC mylib)
Thanks to 'Asteroids With Wings' I figured out that a header-only library should not be added as a library in CMake.
Including the library and linking the XercesC library was enough.
CMake solution
cmake_minimum_required(VERSION 3.17)
project(XercesGebeuren)
set(CMAKE_CXX_STANDARD 20)
set(Xsd_DIR ./cmake)
find_package(XercesC REQUIRED)
find_package(Xsd REQUIRED)
add_executable(XercesGebeuren main.cpp src/hello.cxx )
target_include_directories(XercesGebeuren PUBLIC ${XSD_INCLUDE_DIR})
target_link_libraries(XercesGebeuren PUBLIC XercesC::XercesC)
I have a very small project to which I created some unit testing with GTest.
To build it, I'm using CMake. It compiles just fine, but it's giving me the error shown below. I include all make output just in case.
Scanning dependencies of target trace
[ 33%] Building C object CMakeFiles/trace.dir/trace/system_trace.c.o
[ 33%] Built target trace
Scanning dependencies of target data-test
[ 66%] Building CXX object CMakeFiles/data- test.dir/data_testsuite.cpp.o
[100%] Linking CXX executable data-test
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
CMakeFiles/data-test.dir/build.make:86: recipe for target 'data-test' failed
make[2]: *** [data-test] Error 1
CMakeFiles/Makefile2:72: recipe for target 'CMakeFiles/data-test.dir/all' failed
make[1]: *** [CMakeFiles/data-test.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
And I have no idea why is not finding main. Main is part of GTest which I'm including as it can be seen on the CMakeLists.txt:
cmake_minimum_required(VERSION 2.8)
set (CMAKE_CXX_STANDARD 11)
project(data-test C CXX)
set(CMAKE_C_FLAGS_DEBUG "-g3 -Og -Wall")
set(CMAKE_CXX_FLAGS_DEBUG "-g3 -Og -Wall")
set(CMAKE_C_FLAGS_RELEASE "-g0 -O3 -Wall")
set(CMAKE_CXX_FLAGS_RELEASE "-g0 -O3 -Wall")
include(GoogleTest)
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
set(THREADS_PREFER_PTHREAD_FLAG TRUE)
find_package(Threads REQUIRED)
find_package(GTest REQUIRED)
include_directories(cfg)
include_directories(system)
include_directories(system/include)
include(trace/CMakeLists.txt)
add_executable(data-test
$<TARGET_OBJECTS:trace>
data_testsuite.cpp
)
target_link_libraries(data-test Threads::Threads GTest::GTest)
gtest_discover_tests(data-test)
And as you can see from the make command output, it is compiling just fine.
Do you know what is going on?
The libraries defined by the target GTest::Gtest doesn't contain main function definition. For obtain definition of main you need link with GTest::Main. This is written in documentation.
target_link_libraries(data-test Threads::Threads GTest::Main)
As part of a project I'm working on, I need to make use of the WebKitGTK+ library.
I downloaded the library (tarball) and compiled it as described here.
After the compilation was done:
The lib's headers are in /usr/local/include.
The lib's .so files are in /usr/local/lib.
In my C++ project I tried to add the following CMakeLists.txt file:
cmake_minimum_required(VERSION 3.7)
project(CVE_2016_4657)
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES main.cpp)
add_executable(CVE_2016_4657 ${SOURCE_FILES})
find_package(PkgConfig REQUIRED)
include_directories(/usr/local/include/webkitgtk-4.0)
link_directories(/usr/local/lib/webkit2gtk-4.0)
pkg_check_modules(GTK3 REQUIRED gtk+-3.0)
include_directories(${GTK3_INCLUDE_DIRS})
link_directories(${GTK3_LIBRARY_DIRS})
add_definitions(${GTK3_CFLAGS_OTHER})
pkg_check_modules(SOUP REQUIRED libsoup-2.4)
include_directories(${SOUP_INCLUDE_DIRS})
link_directories(${SOUP_LIBRARY_DIRS})
add_definitions(${SOUP_CFLAGS_OTHER})
target_link_libraries(
CVE_2016_4657
${GTK3_LIBRARIES}
${SOUP_LIBRARIES})
However, when compiling the project I'm getting the following error:
[ 50%] Linking CXX executable CVE_2016_4657
CMakeFiles/CVE_2016_4657.dir/main.cpp.o: In function `main':
/home/idanas/CLionProjects/Switcheroo/main.cpp:17: undefined reference to
`webkit_web_view_get_type'
/home/idanas/CLionProjects/Switcheroo/main.cpp:17: undefined reference to
`webkit_web_view_new'
/home/idanas/CLionProjects/Switcheroo/main.cpp:29: undefined reference to
`webkit_web_view_load_uri'
collect2: error: ld returned 1 exit status
CMakeFiles/CVE_2016_4657.dir/build.make:94: recipe for target
'CVE_2016_4657' failed
make[3]: *** [CVE_2016_4657] Error 1
CMakeFiles/Makefile2:67: recipe for target
'CMakeFiles/CVE_2016_4657.dir/all' failed
make[2]: *** [CMakeFiles/CVE_2016_4657.dir/all] Error 2
CMakeFiles/Makefile2:79: recipe for target
'CMakeFiles/CVE_2016_4657.dir/rule' failed
make[1]: *** [CMakeFiles/CVE_2016_4657.dir/rule] Error 2
Makefile:118: recipe for target 'CVE_2016_4657' failed
make: *** [CVE_2016_4657] Error 2
I have little-to-none experience with CMake and could really use some help.
You are trying to use WebKitGTK+, which is a separate library from both GTK+ and libsoup. You will need to duplicate your pkg_check_modules() code again for WebKitGTK+. You'll need to determine first if you are using WebKit1 or WebKit2 (they have subtly different APIs), and then find the appropriate pkg-config name for that version of WebKitGTK+; check the documentation and the contents of your /usr/lib/pkgconfig directory.
I have a project with the following structure
/cmake_modules/
FindSFML.cmake
/includes/
car.hpp
motor.hpp
tires.hpp
/sources/
car.cpp
motor.cpp
tires.cpp
/main.cpp
/main.hpp
I have the following CMakeFiles.txt file:
cmake_minimum_required(VERSION 2.8)
project (MYGAME)
set (MYGAME_VERSION_MAJOR 1)
set (MYGAME_VERSION_MINOR 0)
set (EXECUTABLE_NAME "mygame")
include_directories ("${MYGAME_BINARY_DIR}")
include_directories ("${MYGAME_BINARY_DIR}/includes")
link_directories ("${MYGAME_BINARY_DIR}/sources")
add_executable(${EXECUTABLE_NAME} main.cpp)
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake_modules" ${CMAKE_MODULE_PATH})
find_package(SFML 2.0 REQUIRED system window graphics network audio)
target_link_libraries(${EXECUTABLE_NAME} ${SFML_LIBRARIES})
When I try to execute make I get this:
[100%] Building CXX object CMakeFiles/mygame.dir/main.cpp.o
Linking CXX executable mygame
CMakeFiles/mygame.dir/main.cpp.o: In function `main':
main.cpp:(.text+0x11): undefined reference to `mynamespace::Car::Instance()'
main.cpp:(.text+0x21): undefined reference to `mynamespace::Car::start()'
collect2: error: ld returned 1 exit status
make[2]: *** [mygame] Error 1
make[1]: *** [CMakeFiles/mygame.dir/all] Error 2
make: *** [all] Error 2
How do I fix it?
You need to include the rest of your sources (car.cpp, motor.cpp and tires.cpp) in the build in some way.
You can either add them along with main.cpp in the executable directly:
set(MySources sources/car.cpp sources/motor.cpp sources/tires.cpp main.cpp)
add_executable(${EXECUTABLE_NAME} ${MySources})
or you can make these into a library and link that:
set(MyLibSources sources/car.cpp sources/motor.cpp sources/tires.cpp)
add_library(MyLib ${MyLibSources})
add_executable(${EXECUTABLE_NAME} main.cpp)
...
target_link_libraries(${EXECUTABLE_NAME} MyLib ${SFML_LIBRARIES})
A couple of other points to note:
You should avoid the use of link_directories if possible (its own documentation discourages its use), and it's often helpful to include the headers in the list of files added via add_executable or add_library since these then show up in IDEs like MS Visual Studio.