How to link ws2_32 in Clion - c++

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

Related

-lboost_system does't help for undefined reference to `boost::system::system_category()' on cygwin

This is my CMakeLists.txt:
set(CMAKE_VERBOSE_MAKEFILE ON)
cmake_minimum_required(VERSION 3.8)
project(spider)
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES main.cpp)
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
add_executable(spider ${SOURCE_FILES})
target_link_libraries(spider ${Boost_LIBRARIES})
target_link_libraries(spider Threads::Threads)
target_link_libraries(spider
${Boost_PROGRAM_OPTIONS_LIBRARIES}
${Boost_FILESYSTEM_LIBRARIES}
${Boost_SYSTEM_LIBRARIES}
)
SET( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -lboost_system -DBOOST_SYSTEM_NO_DEPRECATED" )
This are my includes files from main.cpp:
#include <iostream>
#include <boost/array.hpp>
#include <boost/asio.hpp>
using boost::asio::ip::udp;
I tried downloading and building boost, it causes tons of trobules, so I decided to use boost which comes with cygwin installer. I do have boost_system and boost in my /include folder, so I don't have to setup boost in my Cmakelists.txt:
The question is what to do with the error:
/usr/include/boost/system/error_code.hpp:322: undefined reference to
`boost::system::system_category()'
As you can see, I've tried -lboost_system and -DBOOST_SYSTEM_NO_DEPRECATED, the flags are passed to g++:
/usr/bin/c++.exe -lboost_system -DBOOST_SYSTEM_NO_DEPRECATED -g
-Wl,--enable-auto-import CMakeFiles/spider.dir/main.cpp.o -o spider.exe -Wl,--out-implib,libspider.dll.a
-Wl,--major-image-version,0,--minor-image-version,0
but this doesn't help. Any ideas ?
adding
target_link_libraries(spider boost_system)
instead of SET CMAKE_CXX_FLAGS to the end of cmake file fixes the problem, rootcause is that the flag should be in the end, as commenters noted.
My solution was to put the library in directly instead of -lboost_system.
LIBS= -lboost_thread /usr/local/lib/libboost_system.so.1.68.0
Not sure why boost_system doesn't resolve correctly in < 1.70 (as I didn't have this error with that version of the libraries, using old version to test other issues)
Hope this helps.

How to set library flags after source file in cmake?

I'm trying to compile a project using hwloc with CMake. However, I get a ton of undefined reference errors when linking:
undefined reference to `hwloc_get_type_depth'
undefined reference to `hwloc_bitmap_zero'
[...]
According to this answer to a similar question the order of flags is important.
So, how can I generate a command like this in CMake? :
g++ -Wall -std=c++11 source.cpp-lhwloc
Excerpt from my CMakeLists.txt:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=c++11 -lhwloc")
set(SOURCE_FILES source.cpp)
add_executable(source ${SOURCE_FILES})
Any help is greatly appreciated!
Edit: My question was proposed as a possible duplicate of this one, however the flag I wanted to add was to link against a library and not a normal compile flag as seems to be the case in the above mentioned question. #Edgar Rokyan provided the right answer for my problem.
If you need to link against hwloc library you might use target_link_libraries command:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=c++11") # <== remove *-lhwloc*
set(SOURCE_FILES source.cpp)
add_executable(source ${SOURCE_FILES})
target_link_libraries(source hwloc) # <== add this line

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.

How to link compiled cmph library in my project using CMake

In order to use cmph, a perfect minimal hashing library, in my project organised using CMake, I installed cmph library in a ubuntu machine and tested it using a single c file called main.c.
If I try to compile this file using gcc 5.3.0 using the following command:
gcc main.c
I will get the following output
/tmp/ccSOH5ob.o: In function `main':
testperfect.c:(.text+0x63): undefined reference to `cmph_io_vector_adapter'
testperfect.c:(.text+0x73): undefined reference to `cmph_config_new'
testperfect.c:(.text+0x88): undefined reference to `cmph_config_set_algo'
testperfect.c:(.text+0x9b): undefined reference to `cmph_config_set_mphf_fd'
testperfect.c:(.text+0xa7): undefined reference to `cmph_new'
testperfect.c:(.text+0xb7): undefined reference to `cmph_config_destroy'
testperfect.c:(.text+0xca): undefined reference to `cmph_dump'
testperfect.c:(.text+0xd6): undefined reference to `cmph_destroy'
testperfect.c:(.text+0xe2): undefined reference to `cmph_load'
testperfect.c:(.text+0x118): undefined reference to `cmph_search'
testperfect.c:(.text+0x153): undefined reference to `cmph_destroy'
testperfect.c:(.text+0x15f): undefined reference to `cmph_io_vector_adapter_destroy'
But if I run this command:
gcc main.c $(pkg-config --libs cmph) -o main
It will be compiled and run normally.
Now I need to add a similar piece of code in my project and the CMakeList.txt is written like this:
set(PROJECT_EXECUTABLE ${PROJECT_NAME})
# Compiling flags.
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR
CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(CMAKE_CXX_FLAGS_RELEASE "-O2")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2 -g")
set(CMAKE_CXX_FLAGS_DEBUG "-g")
set(CMAKE_CXX_FLAGS_MINSIZEREL "-Os")
endif()
# Inject project config.
configure_file(
${PROJECT_INCLUDE_DIR}/phsim/config.hpp.in
${PROJECT_INCLUDE_DIR}/phsim/config.hpp
)
include(FindPkgConfig)
find_package(PkgConfig REQUIRED)
pkg_search_module(CMPH REQUIRED cmph)
target_link_libraries(Phsim ${CMPH_LIBRARIES})
target_include_directories(Phsim PUBLIC ${CMPH_INCLUDE_DIRS})
target_compile_options(Phsim PUBLIC ${CMPH_CFLAGS_OTHER})
# Compile executable.
file(GLOB SOURCES ${PROJECT_SRC_DIR}/*.cpp)
add_executable(${PROJECT_EXECUTABLE} ${SOURCES})
set_target_properties(${PROJECT_EXECUTABLE} PROPERTIES
VERSION ${PHSIM_VERSION_LITER}
)
And then I try to run cmake . and make, but only get the error message:
CMake Error at src/CMakeLists.txt:20 (target_link_libraries):
Cannot specify link libraries for target "Phsim" which is not built by this
project.
But I won't get target executable file unless I compile the project. If I try to compile my project without those commands related to library linking, the compiler will give similar link errors provided in the beginning of my question.
I have checked the following questions:
Undefined reference to cmph functions even after installing cpmh library
And I tried instructions provided by these sites:
https://cmake.org/Wiki/CMake:How_To_Find_Libraries
https://cmake.org/cmake/help/v3.6/module/FindPkgConfig.html
Many thanks in advance.
Finally Solved.
cmake_minimum_required(VERSION 3.0.2)
project(TestGamma)
set(GAMMATEST_VERSION_MAJOR 1)
set(GAMMATEST_VERSION_MINOR 0)
set(CMPH_INCLUDE_DIR /usr/local/lib)
include(FindPkgConfig)
configure_file(
"${PROJECT_SOURCE_DIR}/TestGammaConfig.h.in"
"${PROJECT_BINARY_DIR}/TestGammaConfig.h"
)
include_directories(${PROJECT_BINARY_DIR})
set(CMAKE_CXX_FLAGS " ${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_executable(testgamma ${SOURCE_FILES})
pkg_check_modules(CMPH REQUIRED cmph)
include_directories(${CMPH_INDLUDE_DIR})
link_directories(${CMPH_INCLUDE_DIR})
target_link_libraries(testgamma cmph ${CMPH_INCLUDE_DIR})
Make sure to include pkgconfig at first and add link operations after calling "add_executable"
#usr1234567 Thank you for your attention.

libcurl link with mingw and clion

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.