I'm trying to build a simple GTK+ app on Windows (64 bit) using CMake. I've installed everything according to the official guide.
Here's the contents of my CMakeLists.txt:
# Set project
project(gtk-test C)
cmake_minimum_required(VERSION 3.0)
# Configure project paths
set(PROJECT_SOURCE_DIR ${PROJECT_SOURCE_DIR}/src)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/lib)
# Find dependencies
find_package(PkgConfig REQUIRED)
pkg_check_modules(GTK3 REQUIRED gtk+-3.0)
include_directories(${GTK3_INCLUDE_DIRS})
link_directories(${GTK3_LIBRARY_DIRS})
add_definitions(${GTK3_CFLAGS_OTHER})
set(LIBRARIES ${LIBRARIES} ${GTK3_LIBRARIES})
# Compile
add_executable(main ${PROJECT_SOURCE_DIR}/main.c)
target_link_libraries(main ${LIBRARIES})
# Messages
message(STATUS "GTK include directories: ${GTK3_INCLUDE_DIRS}")
and then I'm building the source file with the following:
cmake -Bbuild -H.
cmake --build build
Everything seems to work fine on macOS, but on Windows I keep getting the following error:
fatal error: gtk/gtk.h: No such file or directory
#include <gtk/gtk.h>
I checked the directory included by CMake and the header file is there. Also, the following command from the tutorial successfully builds the application:
gcc `pkg-config --cflags gtk+-3.0` -o main.exe main.c `pkg-config --libs gtk+-3.0`
Still, I would really love to get it working with CMake. I've been searching for the solution for hours now with no result, so any help would be highly appreciated.
Thanks in advance!
Update
Apparently, the whole problem lies within included libraries. For some reason, the line:
include_directories(${GTK3_INCLUDE_DIRS})
does not include them. I managed to fix the problem including libraries myself with -I option:
# Set project
cmake_minimum_required(VERSION 3.0)
project(gtk-test C)
# Configure project paths
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/lib)
set(CMAKE_SOURCE_DIR ${CMAKE_SOURCE_DIR}/src)
# Find dependencies
find_package(PkgConfig REQUIRED)
pkg_check_modules(GTK3 REQUIRED gtk+-3.0)
link_directories(${GTK3_LIBRARY_DIRS})
add_compile_options(${GTK3_CFLAGS_OTHER})
set(LIBRARIES ${LIBRARIES} ${GTK3_LIBRARIES})
set(FLAGS "-I${GTK3_INCLUDE_DIRS}")
message(STATUS "Flags: ${FLAGS}")
string(REPLACE ";" " -I" FLAGS "${FLAGS}")
set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS} ${GTK3_FLAGS} ${FLAGS})
# Compile
add_executable(main ${PROJECT_SOURCE_DIR}/main.c)
target_link_libraries(main ${LIBRARIES})
Although it seems to work, this does not look like a good solution to me.
Related
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
iam trying to build the boost library and use cmake to build my application.
Building and installing boost was just following the Getting Started Guide and changing the prefix to /usr
./bootstrap.sh --prefix=/usr
./b2 install
As result i have now in /usr/lib:
libboost_atomic.a
libboost_atomic.so
libboost_atomic.so.1.64.0
...
And in /usr/include/boost
aligned_storage.hpp
align.hpp
...
My CMakeLists.txt
cmake_minimum_required (VERSION 2.6)
project (NewMediaServer)
# Set the output folder where your program will be created
set(CMAKE_BINARY_DIR ${CMAKE_SOURCE_DIR}/bin)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR})
set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR})
# set the folder of the binarys
include_directories(${PROJECT_BINARY_DIR}/src)
# find all packages which we are depending on
find_package(Boost 1.64 REQUIRED)
# name the main cpp and the executable
add_executable(mediaserver src/MediaServer.cpp)
# configure compile and linking options
target_compile_options(mediaserver PUBLIC -std=c++11 -Wall)
target_link_libraries(mediaserver PUBLIC -pthread -lboost_system -lboost_log -lboost_log_setup -lboost_thread -lboost_date_time -lboost_filesystem)
When i run make everything works fine, but as soon as iam running the binary iam getting the following error ....
./bin/mediaserver: error while loading shared libraries: libboost_system.so.1.64.0: cannot open shared object file: No such file or directory
Really appreciate any help! I'am still new to Cmake and Boost so be gentle ;)
Thanks in advance
#usr1234567 Thanks for the suggestion, was using cmake 2.8 before from CentOS repo. Switched to cmake 3.9, needed to set a link to the binaries default located in /usr/local/bin/cmake. Also builded boost with default prefix. Now it works. Also changed my project like #Tsyvarev suggested to not override the CMAKE_BINARY_DIR. Anytime later i will try it again with prefix building but for now iam fine. Thanks!
cmake_minimum_required(VERSION 3.1)
project(ClanLib)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
SET(ClanLib_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR})
INCLUDE_DIRECTORIES(${ClanLib_INCLUDE_DIRS})
FIND_PACKAGE(ClanLib REQUIRED)
set(SOURCE_FILES main.cpp)
add_executable(ClanLib ${SOURCE_FILES} main.cpp)
TARGET_LINK_LIBRARIES(ClanLib ${ClanLib_LIBRARY})
and cmake give error:
Error:By not providing "FindClanLib.cmake" in CMAKE_MODULE_PATH this project has asked CMake to find a package configuration file provided by "ClanLib", but CMake did not find one.
Could not find a package configuration file provided by "ClanLib" with any of the following names:
ClanLibConfig.cmake clanlib-config.cmake
Add the installation prefix of "ClanLib" to CMAKE_PREFIX_PATH or set "ClanLib_DIR" to a directory containing one of the above files. If "ClanLib" provides a separate development package or SDK, be sure it has been installed.
There is no FindClanLib.cmake provided by ClanLib project or CMake, so find_package will not work in your case.
For Linux you can use pkg-config facility from CMake.
Here the example for ClanLib usage on Linux.
I hope someone is able to help me:
I try to use the cmake plugin in a jenkins server to create a hex file, but i don't know my fault.
I also try to run the example of this site: http://robot-develop.org/archives/2952 but if i use the "make" command, i get this error message:
make: *** No targets specifed and no makefile found. Stop
Here is my code:
CMakeLists.txt:
cmake_minimum_required (VERSION 2.6.0)
INCLUDE("C:/Users/name/Desktop/example/Code/test_crosscompile.cmake")
INCLUDE("C:/Users/name/Desktop/example/Code/macro.cmake")
project (Code)
MESSAGE(STATUS "\n -------------------------------->Creating Test-Projekt...")
IF( NOT CMAKE_BUILD_TYPE )
SET( CMAKE_BUILD_TYPE Release ... FORCE )
ENDIF()
SET(MAIN_FILE
class.c)
# add the executable
add_executable(Code ${MAIN_FILE})
#makro
AVR_CREATE_HEX(class)
makro.cmake:
# create avr hex
MACRO(AVR_CREATE_HEX name)
ADD_CUSTOM_COMMAND(TARGET ${name} POST_BUILD COMMAND avr-objcopy ARGS -O ihex -R.eeprom ${name} "C:/Users/name/Desktop/example/Code/class.hex")
MESSAGE(STATUS "\n ----------JONAS---------------------->Macro")
ENDMACRO(AVR_CREATE_HEX)
test_crosscompile.cmake:
SET(CMAKE_SYSTEM_NAME "Windows")
SET(CMAKE_C_COMPILER avr-gcc)
SET(CMAKE_CXX_COMPILER avr-g++)
SET(CSTANDARD "-std=gnu99")
SET(CDEBUG "-gstabs")
SET(CWARN "-Wall -Wstrict-prototypes")
SET(CTUNING "-funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums")
SET(COPT "-Os")
SET(CMCU "-mmcu=atmega32")
SET(CDEFS "-DF_CPU=12000000UL")
SET(CFLAGS "${CMCU} ${CDEBUG} ${CDEFS} ${CINCS} ${COPT} ${CWARN} ${CSTANDARD} ${CEXTRA}")
SET(CXXFLAGS "${CMCU} ${CDEFS} ${CINCS} ${COPT}")
SET(CMAKE_C_FLAGS ${CFLAGS})
SET(CMAKE_CXX_FLAGS ${CXXFLAGS})
I use the cmake GUI, but i don't know how to use it.
I try as generator visual studio 10 (specify toolchain file for cross compiling), but it generates no makefile or hex file.
If i choose MinGW Makefiles as the generator, i get the error:
The C compiler "C:/WinAVR-20100110/bin/avr-gcc.exe" is not able to compile
a simple test program.
I hope somebody can help me, THANKS!
You have the CMAKE_SYSTEM_NAME set to windows, you need to set this to generic to enable Cross compiling eg:
SET(CMAKE_SYSTEM_NAME Generic)
I'm writing a Python extension module in C++ using Boost.Python. However, I
would like to use a newer version of the Boost library than the system
installation offers. This newer version of boost is contained in
BOOST_ROOT=$HOME/opt/boost/1.55.0.
Following this guide on how
to use RPath in CMake I came up with the following CMakeLists.txt.
cmake_minimum_required(VERSION 2.8)
project("test")
set(PROJECT_DESC "Test Boost.Python")
set(CMAKE_SKIP_BUILD_RPATH FALSE)
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
add_definitions(-std=c++11 -Wall -Wextra -pedantic)
find_package(PythonInterp REQUIRED)
find_package(PythonLibsNew REQUIRED)
find_package(Boost COMPONENTS python REQUIRED)
message(STATUS "Using Boost installation in:")
message(STATUS " INCLUDE: ${Boost_INCLUDE_DIRS}")
message(STATUS " LIB: ${Boost_LIBRARIES}")
include_directories(
${PROJECT_SOURCE_DIR}
${PYTHON_INCLUDE_DIRS}
${Boost_INCLUDE_DIRS}
)
macro(add_python_module _name _srccpp)
PYTHON_ADD_MODULE(${_name} ${_srccpp})
target_link_libraries(${_name} ${Boost_LIBRARIES})
endmacro()
add_python_module(ownership ownership.cpp)
Then I run the following commands to build the module
mkdir build; cd build
cmake -DCMAKE_INSTALL_PATH="$BOOST_ROOT/lib" ..
make
The status message after running cmake points to the right boost
installation. (The CMake boost module picks up the environment variable
$BOOST_ROOT) I.e. the CMake variable Boost_LIBARIES points to
$BOOST_ROOT/lib/libboost_python.so.
But, if I check which libraries would actually be used, the system libraries
are listed:
$ ldd ownership.so
# ...
libboost_python.so.1.53.0 => /usr/lib64/libboost_python.so.1.53.0 (0x00007f09abfc1000)
# ...
This is version 1.53, even though the status message above explicitely pointed
to 1.55.
What am I doing wrong? How can I get ldd to pick the library in
$BOOST_ROOT/lib/libboost_python.so.1.55.0?
First of all as I already mentioned in commens you don't need to use CMake RPATH-manipulations
options. Example: http://pastebin.com/UDyYbQ1d, output: standard and custom
Do you know of a way of convincing CMake otherwise even if LIBRARY_PATH is set
This issue is not related to CMake, it's compiler responsibility. Read this discussion.
Solution
You can clear LIBRARY_PATH if you set BOOST_ROOT variable explicitly. And you can check
environment variable in CMakeLists.txt to avoid this problem in future:
string(COMPARE NOTEQUAL "$ENV{LIBRARY_PATH}" "" library_path_warning)
if(library_path_warning)
message(
WARNING
"LIBRARY_PATH environment variable is not empty ($ENV{LIBRARY_PATH}) "
"This may cause dynamic linking errors!"
)
endif()