C++ 64bit project with PDCurses static link library via Clion's CMake - c++

Recently I have acquired the "curses.h" and built the PDCurses "pdcurses.a" library file thanks to:
https://github.com/Alexpux/MINGW-packages/tree/master/mingw-w64-pdcurses
package. I also have prepared cmake files:
# pdcurses-config.cmake
set(PDCURSES_LIBDIR "${PROJECT_SOURCE_DIR}/lib")
set(PDCURSES_INCLUDE_DIRS "${PROJECT_SOURCE_DIR}/src/include")
set(PDCURSES_LIBRARIES "-L${PDCURSES_LIBDIR} -lpdcurses -static -Wall -Werror")
string(STRIP "${PDCURSES_LIBRARIES}" PDCURSES_LIBRARIES)
# CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(MatrixAlgebra)
set(CMAKE_CXX_STANDARD 11)
set(PDCURSES_DIR "${PROJECT_SOURCE_DIR}/cmake")
find_package(PDCURSES REQUIRED)
include_directories(${PDCURSES_INCLUDE_DIRS})
set(SOURCE_FILES src/main.cpp)
add_executable(MatrixAlgebra ${SOURCE_FILES})
target_link_libraries(MatrixAlgebra ${PDCURSES_LIBRARIES})
Unfortunately I'm unable to link a simple "Hello World!" console program because either I am getting this:
mingw32/7.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lpdcurses
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: * [CMakeFiles\MatrixAlgebra.dir\build.make:97: MatrixAlgebra.exe] Error 1
mingw32-make.exe[2]: [CMakeFiles\Makefile2:67: CMakeFiles/MatrixAlgebra.dir/all] Error 2
mingw32-make.exe[1]: [CMakeFiles\Makefile2:79: CMakeFiles/MatrixAlgebra.dir/rule] Error 2
mingw32-make.exe: * [Makefile:117: MatrixAlgebra] Error 2
or this (when I change "pdcurses.a" to "libpdcurses.a"):
Process finished with exit code -1073741515 (0xC0000135)
I simply don't know what to do to make it proceed without problems.

you shouldn't treat target_link_libraries() like commandline to feed it with parameters like -Wall
I don't know pdcurses, but when find_package finds that lib you should probably use something like:
target_link_libraries(MatrixAlgebra pdcurses::pdcurses)

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...

How to link libraries properly using CMakeLists.txt for MinGW?

I have been pulling out of my hair because of CMakelists.txt. I would like to create a C++ application with GLFW, GLEW libraries, using MinGW and CLion IDE.
Here is my CMakeLists.txt:
cmake_minimum_required(VERSION 3.17.3)
project(imguiTask)
set(CMAKE_CXX_STANDARD 17)
add_compile_options(-DGLEW_NO_GLU)
add_executable(${PROJECT_NAME}
src/main.cpp
src/imgui.cpp
src/imgui_draw.cpp
src/imgui_widgets.cpp
)
target_include_directories(${PROJECT_NAME} PUBLIC includes)
add_library(libraries STATIC IMPORTED)
target_link_libraries(${PROJECT_NAME} glfw3)
I placed the needed libraries in the libraries folder as you can see in the picture. The folder is located in the project root. It is giving me the following error:
d:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: cannot find -lglfw3
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [imguiTask.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/imguiTask.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/imguiTask.dir/rule] Error 2
mingw32-make.exe: *** [imguiTask] Error 2
It seeems to me, that it does not search in the folder I specified. I don't know why.

cmake or g++: include dll libraries

I have been trying to write a code in C++ under Windows, that uses a third party library (I have .dll files, .lib files and .h files). I tried so many configurations in CMAKE (I am using CLion), but none of them worked! still cannot find the library. So I decided to use the plain g++ command. Still nothing!! The image below shows file tree
This is my latest cmakelists.txt:
cmake_minimum_required(VERSION 3.12)
project(tf_cert_mngr)
set(CMAKE_CXX_STANDARD 11)
set(LIBRARIES_DIR ${PROJECT_SOURCE_DIR}/openssl)
include_directories(src)
link_directories(${LIBRARIES_DIR}/lib)
include_directories(${LIBRARIES_DIR}/include)
#include_directories(include openssl\\include openssl\\lib2)
set(SOURCE_FILES src/Certificate.cpp src/tf-cert-mngr.cpp)
set(HEADER_FILES include/Certificate.h include/tf-cert-mngr.h)
find_package(ssl )
find_package(crypto)
add_executable(tf_cert_mngr ${SOURCE_FILES} ${HEADER_FILES})
target_link_libraries(tf_cert_mngr libssl.lib libcrypto.lib)
its result:
[ 33%] Linking CXX executable tf_cert_mngr.exe
c:/mingw/bin/../lib/gcc/mingw32/5.3.0/../../../../mingw32/bin/ld.exe: cannot find -lssl
c:/mingw/bin/../lib/gcc/mingw32/5.3.0/../../../../mingw32/bin/ld.exe: cannot
find -lcrypto
collect2.exe: error: ld returned 1 exit status
CMakeFiles\tf_cert_mngr.dir\build.make:101: recipe for target 'tf_cert_mngr.exe' failed
mingw32-make.exe[3]: *** [tf_cert_mngr.exe] Error 1
CMakeFiles\Makefile2:71: recipe for target 'CMakeFiles/tf_cert_mngr.dir/all' failed
mingw32-make.exe[2]: *** [CMakeFiles/tf_cert_mngr.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/tf_cert_mngr.dir/rule] Error 2
CMakeFiles\Makefile2:83: recipe for target 'CMakeFiles/tf_cert_mngr.dir/rule'
failed
Makefile:117: recipe for target 'tf_cert_mngr' failed
mingw32-make.exe: *** [tf_cert_mngr] Error 2
and this is the latest g++ command I used:
g++ src\tf-cert-mngr.cpp src\Certificate.cpp -Iinclude -I openssl\include -L openssl\lib -llibcrypto.lib -l libssl.lib -o tester
its result:
c:/mingw/bin/../lib/gcc/mingw32/5.3.0/../../../../mingw32/bin/ld.exe: cannot find -llibcrypto.lib
c:/mingw/bin/../lib/gcc/mingw32/5.3.0/../../../../mingw32/bin/ld.exe: cannot find -llibssl.lib
collect2.exe: error: ld returned 1 exit status
Have a look at the documentation for FindOpenSSL.cmake. This is the script that is called internally when you call:
find_package(OpenSSL) (note that for portability, case is important).
Notice that the script creates two targets:
This module defines the following IMPORTED targets:
OpenSSL::SSL
    The OpenSSL ssl library, if found.
OpenSSL::Crypto
    The OpenSSL crypto library, if found.
So try changing your CMakeLists.txt file to read:
find_package(OpenSSL REQUIRED)
...
target_link_libraries(tf_cert_mngr OpenSSL::SSL OpenSSL::Crypto)
Reference: https://cmake.org/cmake/help/latest/module/FindOpenSSL.html

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!

cmake link xlib directories c++

I'm trying to compile a c++ program that uses xlib with cmake. However, I'm having a problem including and linking xlib libraries in cmake file.
This is the error that I'm getting.
main.cpp:378: undefined reference to `XClearWindow'
collect2: error: ld returned 1 exit status
CMakeFiles/project1.dir/build.make:94: recipe for target 'project1' failed
make[2]: *** [project1] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/project1.dir/all' failed
make[1]: *** [CMakeFiles/project1.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
And when I use just the command line to compile, it works just fine.
I use this command (g++ main.cpp -L/usr/X11R6/lib -lX11)
and this is my cmake file.
cmake_minimum_required(VERSION 3.6)
project(project1)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
link_directories(/usr/X11R6/lib)
include_directories(/usr/share/X11)
set(SOURCE_FILES main.cpp)
add_executable(project1 ${SOURCE_FILES})
In your case, you forgot to specify the libraries that cmake should use to link your application (target_link_libraries or link_libraries).
But, why not just let cmake find the required path, libraries and includes by itself? I suggest you to use find_package(X11). In your case, you can try:
cmake_minimum_required(VERSION 3.6)
project(project1)
set(CMAKE_CXX_STANDARD 11) # for c++11
find_package(X11 REQUIRED)
link_libraries(${X11_LIBRARIES})
include_directories(${X11_INCLUDE_DIR})
set(SOURCE_FILES main.cpp)
add_executable(project1 ${SOURCE_FILES})
I won't be able to explain why (if you have some idea, don't hesitate to comment my answer), but, in my case, I had to link X11 library using this command:
target_link_libraries( DisplayImage "-lX11" )
It works, at least, for the 3.5.1 version of cmake.