libcurl link with mingw and clion - c++

I'm trying to build my project using curl, but I have this result :
undefined reference to `_imp__curl_easy_init'
This is my CMakeLists :
cmake_minimum_required(VERSION 2.8)
project(score)
set(SOURCE_FILES main.cpp)
add_executable(score ${SOURCE_FILES})
add_library(libcurl STATIC IMPORTED)
set_property(TARGET libcurl PROPERTY IMPORTED_LOCATION "c:/MinGW/lib")
SET(GCC_COVERAGE_LINK_FLAGS "-lcurl")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
SET( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${GCC_COVERAGE_LINK_FLAGS}" )
Moreover, I put my file libcurl.a, etc... in the correct directory "c:/MinGW/lib".
Could you help me ?

You probably need to compile the source files that call the curl functions with the CURL_STATICLIB macro defined.
Do you have access to the curl-config utility? It's there when you build curl from source. Run it with the --cflags option to get the compiler flags required and the --libs option to get linker requirements.
For example, in my mingw environment, the cflags reported are -DCURL_STATICLIB -I/mingw/local/include and the lib flags reported are -L/mingw/local/lib -lcurl -lssl -lcrypto -lgdi32 -lwldap32 -lz -lws2_32.

Related

Linking with thrift

I'm getting undefined reference to 'typeinfo for apache::thrift::transport::TTransportException' (and other symbols) when trying to link my executable with my library which uses thrift. I'm using GCC 7.3.0 on Ubuntu 18.04, building with CMake in CLion
I'm stuck after spending a day googling about this problem and after visiting this, that and this links.
The CMake command looks as following
For my shared object:
TARGET_LINK_LIBRARIES(server INTERFACE
etcdclient
TopologyProtocols
event
${THRIFT_LIBRARIES}
${Boost_LIBRARIES}
lzo2
sqlite3
zmq
${SPDK_LIBS}
${DPDK_LIBS}
grpc
grpc++
gtest
gmock
xml2
stdc++fs
bfd
-l:libisal.so.2
sgutils2
pthread
uuid
rt
)
The executable CMake command:
TARGET_LINK_LIBRARIES(kserver
server
${THRIFT_LIBRARIES}
)
Linker command generated by CMake:
cmake_link_script CMakeFiles/kserver.dir/link.txt --verbose=1
/usr/bin/c++ -O3 -DNDEBUG CMakeFiles/kserver.dir/main.cpp.o -o kserver -L/server/ext/spdk/build/lib -L/server/ext/spdk/dpdk/build/lib -L/server/ext/isal/lib -Wl,-rpath,/server/ext/spdk/build/lib:/server/ext/spdk/dpdk/build/lib:/server/ext/isal/lib:/server/cmake-build-release/lib/proj:/usr/local/lib:/server/cmake-build-release/ext/etcd:/server/cmake-build-release/protocols ../../lib/proj/libproj.so /usr/local/lib/libthrift.so /usr/local/lib/libthriftnb.so ../../ext/etcd/libetcdclient.so ../../protocols/libTopologyProtocols.so /home/user/vcpkg/installed/x64-linux/lib/libprotobuf.a -levent /usr/local/lib/libthrift.so /usr/local/lib/libthriftnb.so /home/user/vcpkg/installed/x64-linux/lib/libboost_system.a -llzo2 /home/user/vcpkg/installed/x64-linux/lib/libsqlite3.a -lpthread -ldl -lzmq -lspdk -ldpdk -lgrpc -lgrpc++ -lgtest -lgmock -lxml2 -lstdc++fs -lbfd -l:libisal.so.2 -lsgutils2 -lpthread -luuid -lrt
The command looks perfectly fine, it links with thrift, thriftnb and event
As of compiler and general projects settings, here a content of root CMakeList.txt in the source root
LIST(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
LIST(APPEND CMAKE_PREFIX_PATH "${CMAKE_SOURCE_DIR}/cmake")
SET(CMAKE_CXX_STANDARD 17)
#SET(ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/build/lib")
#SET(LIBRARY_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/build/lib")
#SET(RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/build/bin")
ADD_COMPILE_OPTIONS(
-include server.h
# -Wall
# -pedantic
-march=native
)
ADD_COMPILE_DEFINITIONS(
BOOST_COROUTINES_NO_DEPRECATION_WARNING
GTEST_LINKED_AS_SHARED_LIBRARY
)
INCLUDE_DIRECTORIES(
${CMAKE_SOURCE_DIR}/lib/include/server
${CMAKE_SOURCE_DIR}/lib/include
)
LINK_DIRECTORIES(
${CMAKE_SOURCE_DIR}/ext/spdk/build/lib
${CMAKE_SOURCE_DIR}/ext/spdk/dpdk/build/lib
${CMAKE_SOURCE_DIR}/ext/isal/lib
)
FIND_PACKAGE(Boost REQUIRED COMPONENTS
system)
FIND_PACKAGE(Protobuf REQUIRED)
FIND_PACKAGE(GRPC REQUIRED)
INCLUDE(${CMAKE_CURRENT_LIST_DIR}/cmake/FindGRPC.cmake)
FIND_PACKAGE(LZO REQUIRED)
FIND_PACKAGE(sqlite3 REQUIRED)
FIND_PACKAGE(ZeroMQ CONFIG REQUIRED)
FIND_PACKAGE(GTest REQUIRED)
FIND_PACKAGE(Thrift REQUIRED)
ADD_SUBDIRECTORY(lib/server)
ADD_SUBDIRECTORY(ext/etcd)
ADD_SUBDIRECTORY(protocols)
ADD_SUBDIRECTORY(proc/kserver)
Sample compilation command
/usr/bin/c++ -DBOOST_COROUTINES_NO_DEPRECATION_WARNING -DGTEST_LINKED_AS_SHARED_LIBRARY -Dserver_EXPORTS -I/home/user/sourcelib/include/server -I/home/user/sourcelib/include -I/home/user/sourceext/spdk/include -I/home/user/sourcecmake-build-release/ext/etcd -I/home/user/sourcecmake-build-release/protocols -I/home/user/vcpkg/installed/x64-linux/include -O3 -DNDEBUG -fPIC -include server.h -march=native -std=gnu++1z -o CMakeFiles/server.dir/misc/ServerHost.cpp.o -c /home/user/sourcelib/server/misc/ServerHost.cpp
What I'm doing wrong?
It took two days to figure out that there was some esoteric version installed on my machine and the package manager was not aware of it, after removing it manually and using version provided by vcpkg everything linked as expected.

How to statically link Qt5 with CMake in CLion?

I downloaded the sources of Qt and compiled it with -static flag into /usr/local/Qt5_static/. I created empty project and changed CMakeLists.txt like this:
cmake_minimum_required(VERSION 3.6)
project(Splines)
set(CMAKE_CXX_COMPILER "/usr/bin/clang++")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -Wall -Wextra -pedantic -static -stdlib=libc++ -lc++abi -v")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")
set(SOURCE_FILES main.cpp)
set(CMAKE_AUTOMOC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(Qt5_DIR "/usr/local/Qt5_static/")
find_package(Qt5Widgets)
add_executable(Splines ${SOURCE_FILES})
target_link_libraries(Splines Qt5::Widgets)
There is a linker error.
/usr/bin/ld: attempted static link of dynamic object `/usr/lib/libQt5Widgets.so.5.7.0'
clang-3.8: error: linker command failed with exit code 1 (use -v to see invocation)
Looks like linker tried to link shared lib instead of static lib i compiled. I tried other variations of CMakeLists.txt but failed.
How to statically link Qt with cmake properly?
(Sorry for my bad english)
CMAKE_CXX_FLAGS are compiler flags, but target_link_libraries uses linker flags, which are CMAKE_EXE_LINKER_FLAGS. For static link you need to add -static to linker flags.

FreeType not linking with CMake after explicit path to library given.

I have an issue with linking FreeType with my project. I have my libs in a separate folder, {PROJECT}/lib, where libfreetype.a is located. My compiler is MinGW-64 4.8.3 x86_64-posix-seh-rev2, and I built libfreetype.a from source using this compiler. My cmake file looks like the following:
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/Build/Debug")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -lmingw32")
set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++")
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
include_directories(${PROJECT_SOURCE_DIR}/include)
link_directories(${PROJECT_SOURCE_DIR}/lib)
set(FREETYPE_INCLUDE_DIRS ${PROJECT_SOURCE_DIR}/include/)
set(FREETYPE_LIBRARY ${PROJECT_SOURCE_DIR}/lib/libfreetype.a)
find_package(Freetype REQUIRED)
set(SOURCE_FILES main.cpp ...)
add_executable(Wahoo ${SOURCE_FILES})
target_link_libraries(Test freetype)
Running this will cause the linker to fail with undefined references to '__imp_FT_Init_FreeType'. Removing the the libfreeType.a causes CMAKE say that it cannot find -lfreeType (as expected), but including libfreetype.a will cause the linker errors.
I have to change my answer, because I tried it on my own and it was bullshit.
I think, you have to change only your last line into
target_link_libraries(Test ${FREETYPE_LIBRARY})

How to link ws2_32 in Clion

I am using Clion, which uses MinGW and Cmake. When I try to use the standalone asio library I am getting
undefined reference to `WSAStartup#8'
undefined reference to `WSASetLastError#4'
undefined reference to `closesocket#4'
...
I believe I have to link the C:/Windows/System32/ws2_32.dll library. I tried adding something like -L C:/Windows/System32 -lws2_32:
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${GCC_COVERAGE_LINK_FLAGS} -static -lws2_32")
But that didn't help. How can I fix these errors ?
The following CMakeLists.txt compiled error-less. Only 1 line is really required: link_libraries(ws2_32 wsock32)
cmake_minimum_required(VERSION 3.3)
project(server_client)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -s -O3 -I C:/Users/Shiro/Desktop/asio-1.10.6/include")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${GCC_COVERAGE_LINK_FLAGS} -static")
link_libraries(ws2_32 wsock32)
set(SOURCE_FILES chat_server.cpp)
add_executable(server_client ${SOURCE_FILES})

How to set up CmakeList flags

This is the way how I compile my cpp code:
g++ -I/usr/local/include/modbus `pkg-config glib-2.0 --cflags --libs` -L/usr/local/lib -lmodbus test-modbus.c -o test-modbus
with gcc it is working pretty fine. But I need to place it into CmakeList.txt and I tried this:
SET(GCC_COVERAGE_COMPILE_FLAGS "-I/usr/local/include/modbus -L/usr/local/lib")
SET(GCC_COVERAGE_LINK_FLAGS "-lmodbus")
SET( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}" )
SET( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${GCC_COVERAGE_LINK_FLAGS}" )
But with no success. Any ideas?
CMake aims to be platform- and compiler-independent, so you don't set specific compiler flags, but you tell CMake what you want to do. For example, the include_directories command adds directories to the include path (similar to gcc's -I option), target_link_libraries links a library (similar to gcc's -l) option, pkg_config can be called through the PkgConfig module, and so on.