I have the following CMakeLists.txt:
cmake_minimum_required(VERSION 3.3)
project(Thesis)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp Graph.h Graph.cpp)
add_executable(Thesis ${SOURCE_FILES})
I am using Run->Build (as release) on a custom folder ClionProjects\Thesis\exe\Release and I get a single executable Thesis.exe. If I open that, I get the following consecutive errors:
What am I missing exactly ?
My solution was to link the libraries statically. That was there is no need for an awkward .dll standing next to your .exe.
Adding a single line on the CMakeLists.txt
set(CMAKE_EXE_LINKER_FLAGS -static)
Fixed my problem. Here is other 2 options that also work, in case you need it for some reason.
#set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${GCC_COVERAGE_LINK_FLAGS} -static-libgcc -static-libstdc++ -static")
#set(CMAKE_EXE_LINKER_FLAGS=-static-libgcc -static-libstdc++ -static)
My .exe went from 100KB to 1MB
Edit: A couple more cool options
Added -s and -O3 to my original CMakeLists.txt of my question.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -s -O3")
-s reduced size from 1MB to 650KB. -s
-O3 is supposed to set optimization level to 3 which is the max -O3
You can see all the options from the gcc.gnu.org site. There are too many. Use the "find" option of your browser (Ctrl + f).
You are missing 2 of the required DLLs.
The easiest way to resolve this is to tell the compiler to link with every library statically by using the -static option in GCC.
Another way is to copy those DLLs in the folder in which your executable exists.
The third way is to find those DLLs and register them.
Related
When I was using Github Action CI, I found that no matter what method I used to link, there was no way to link pthread_create
But this error only appears in the Ubuntu environment, Windows, macOS are no problem
I tried:
Not Working
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
set(THREADS_PREFER_PTHREAD_FLAG TRUE)
find_package(Threads REQUIRED)
add_executable(xxx xxx.c)
target_link_libraries(xxx PRIVATE Threads::Threads)
Not Working
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread")
You can view the compiled log here:
https://github.com/YuzukiTsuru/lessampler/runs/6640320037?check_suite_focus=true
If you read the build log carefully
/usr/bin/ld: CMakeFiles/GenerateAudioModelTest.dir/__/src/GenerateAudioModel.cpp.o: in function `GenerateAudioModel::GenerateModelFromFile()':
GenerateAudioModel.cpp:(.text+0x27aa): undefined reference to `pthread_create'
You notice the error has happened while linking the target GenerateAudioModelTest that is located in the directory test and CMakeLists.txt there does not have the compiler flags you shown. Just add -pthread in test/CMakeLists.txt.
This is a bad idea.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
Use
target_compile_options(GenerateAudioModelTest PRIVATE -pthread)
See What is the modern method for setting general compile flags in CMake?
Not Working
You did not link with Thread::Thread nor any library that the target links to.
https://github.com/YuzukiTsuru/lessampler/blob/a6bb7e7d7ac30b6b4043d4f717a2d4deb7fb7638/test/CMakeLists.txt#L22
Not Working
Flags have to be set before add_executable. Which means before all the add_subdirectories. And flags have directory scope. Use targe_compile_options.
https://github.com/YuzukiTsuru/lessampler/blob/master/src/CMakeLists.txt
Consider just making it one library, why so many, and so many CMakeLists.txt in every directory. If the tools are not so separate and you are never going to use them separately, just make it one library with one CMakeLists.txt that links with all the libraries.
Work on Ubuntu 16
I used g++ main.cpp -lpq command for compiler my small project. Now I use Clion and wanna do same what I do with g++. But I can't add compiler flags in cmake file and get compile error.
cmake_minimum_required(VERSION 3.5.1)
project(day_g)
set(CMAKE_CXX_FLAGS "-lpq")
add_definitions(-lpq)
message("CMAKE_CXX_FLAGS is ${CMAKE_CXX_FLAGS}")
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES main.cpp)
add_executable(day_g ${SOURCE_FILES})
Also I run only cmake file and get CMAKE_CXX_FLAGS with -lpq flag.
CMAKE_CXX_FLAGS is -lpq
-- Configuring done
-- Generating done
How properly add compiler flags to cmake file?
Flag -l is for linker, not for compiler. This flag is used for link with libraries. CMake has special command target_link_libraries for that purpose:
target_link_libraries(day_g pq)
-lq is not a compiler flag (CFLAGS) but a linker flag.
To pass a library in a CMake project you should use:
target_link_libraries(target_name libraries...)
Note that if you specify 'q' as library the project will link with libq.a or, if you are on windows q.dll.
... in your CMakeLists.txt the correct line to add is:
target_link_libraries(day_g pq)
Note also that when you add a CFLAG you should also "remember" the previous ones that may be added by libraries or by your platform, ie:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3")
To check the exact flags cmake is passing to compiler or linker you can always run, from the build directory, the following command:
make VERBOSE=1
Work on Ubuntu 16
I used g++ main.cpp -lpq command for compiler my small project. Now I use Clion and wanna do same what I do with g++. But I can't add compiler flags in cmake file and get compile error.
cmake_minimum_required(VERSION 3.5.1)
project(day_g)
set(CMAKE_CXX_FLAGS "-lpq")
add_definitions(-lpq)
message("CMAKE_CXX_FLAGS is ${CMAKE_CXX_FLAGS}")
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES main.cpp)
add_executable(day_g ${SOURCE_FILES})
Also I run only cmake file and get CMAKE_CXX_FLAGS with -lpq flag.
CMAKE_CXX_FLAGS is -lpq
-- Configuring done
-- Generating done
How properly add compiler flags to cmake file?
Flag -l is for linker, not for compiler. This flag is used for link with libraries. CMake has special command target_link_libraries for that purpose:
target_link_libraries(day_g pq)
-lq is not a compiler flag (CFLAGS) but a linker flag.
To pass a library in a CMake project you should use:
target_link_libraries(target_name libraries...)
Note that if you specify 'q' as library the project will link with libq.a or, if you are on windows q.dll.
... in your CMakeLists.txt the correct line to add is:
target_link_libraries(day_g pq)
Note also that when you add a CFLAG you should also "remember" the previous ones that may be added by libraries or by your platform, ie:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3")
To check the exact flags cmake is passing to compiler or linker you can always run, from the build directory, the following command:
make VERBOSE=1
Work on Ubuntu 16
I used g++ main.cpp -lpq command for compiler my small project. Now I use Clion and wanna do same what I do with g++. But I can't add compiler flags in cmake file and get compile error.
cmake_minimum_required(VERSION 3.5.1)
project(day_g)
set(CMAKE_CXX_FLAGS "-lpq")
add_definitions(-lpq)
message("CMAKE_CXX_FLAGS is ${CMAKE_CXX_FLAGS}")
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES main.cpp)
add_executable(day_g ${SOURCE_FILES})
Also I run only cmake file and get CMAKE_CXX_FLAGS with -lpq flag.
CMAKE_CXX_FLAGS is -lpq
-- Configuring done
-- Generating done
How properly add compiler flags to cmake file?
Flag -l is for linker, not for compiler. This flag is used for link with libraries. CMake has special command target_link_libraries for that purpose:
target_link_libraries(day_g pq)
-lq is not a compiler flag (CFLAGS) but a linker flag.
To pass a library in a CMake project you should use:
target_link_libraries(target_name libraries...)
Note that if you specify 'q' as library the project will link with libq.a or, if you are on windows q.dll.
... in your CMakeLists.txt the correct line to add is:
target_link_libraries(day_g pq)
Note also that when you add a CFLAG you should also "remember" the previous ones that may be added by libraries or by your platform, ie:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3")
To check the exact flags cmake is passing to compiler or linker you can always run, from the build directory, the following command:
make VERBOSE=1
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