WIN32 and UNIX don't change in CMake cross-compile - c

I made a test CMakeLists.txt. I set system name and version before reading variables (although I didn't set the compiler):
cmake_minimum_required(VERSION 3.10)
project(test)
include_directories(.)
set(CMAKE_SYSTEM_NAME Windows)
set(CMAKE_SYSTEM_VERSION 10.0.10240.0)
add_executable(test test.c)
target_link_libraries(test)
message("cmake system name = ${CMAKE_SYSTEM_NAME}")
message("cmake host name = ${CMAKE_HOST_SYSTEM_NAME}")
message("cmake system version = ${CMAKE_SYSTEM_VERSION}")
message("unix = ${UNIX}")
message("win32 = ${WIN32}")
Here is the output from "cmake ." on the terminal:
-- The C compiler identification is GNU 9.4.0
-- The CXX compiler identification is GNU 9.4.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
cmake system name = Windows
cmake host name = Linux
cmake system version = 10.0.10240.0
unix = 1
win32 =
-- Configuring done
-- Generating done
-- Build files have been written to: /home/francisco/Documents
The CMake documentation says UNIX and WIN32 are set to the target OS. Here I set the target OS and yet they remain set to the host OS. Why?

You're not supposed to write CMAKE_SYSTEM_NAME after the first project() command is encountered by CMake. At the first project command the choices of compiler and target system are made and you cannot change the effects of this later. As you have seen you can overwrite the value of CMAKE_SYSTEM_NAME, but the only effect of doing this are that you and any external cmake logic that may be invoked (e.g. scripts executed when using find_package) see a value that is wrong (i.e. not matching the choice of compiler).
For setting this kind of information your CMakeLists.txt files are the wrong place. This kind of info belongs into a toolchain file alongside your choice of compiler that actually produces binaries for the target platform:
toolchain-windows.cmake
set(CMAKE_SYSTEM_NAME Windows)
set(CMAKE_SYSTEM_VERSION 10.0.10240.0)
set(CMAKE_C_COMPILER <command/absolute path to executable to use as C compiler goes here>)
set(CMAKE_CXX_COMPILER <command/absolute path to executable to use as C++ compiler goes here>)
...
Configure the project using something like
cmake --toolchain path/to/toolchain-windows.cmake -S path/to/source -B path/to/build/dir
Note: The default compiler on your linux almost certainly won't be able cross compile for windows targets.

Related

cMake to compile C source files with g++ compiler

I have been asked to compile C Source codes with g++ compiler in cMake environment.
I tried one of the solution from Stack Overflow:
Set CC,CXX before running cmake like in below steps.
export CC=/usr/bin/g++
export CXX=/usr/bin/g++
but I get below errors while running cmake
"cmake -DPLATFORM=x64 ../"
-- The C compiler identification is unknown
-- The CXX compiler identification is GNU 7.5.0
-- Check for working C compiler: /usr/bin/g++
-- Check for working C compiler: /usr/bin/g++ -- broken
CMake Error at /usr/share/cmake-3.10/Modules/CMakeTestCCompiler.cmake:52 (message):
The C compiler
"/usr/bin/g++"
is not able to compile a simple test program.
It fails with the following output:
For individual files you can set the language yourself independent of the file extension by setting the LANGUAGE source file property:
# compile foo.c and bar.c with the C++ compiler
set_source_files_properties(foo.c bar.c PROPERTIES LANGUAGE CXX)
This results in CMake using the C++ compiler for the .c files regardless of the compilers used for C / C++.
It may be possible to achieve the same effect for the whole project by listing only C++ as language, but I haven't tested this option:
# top level project
project(MyProject LANGUAGES CXX)
...

CMAKE dont find the libmpdclient-2.13 library

I´m new to cmake and i need help to build the ympd https://github.com/notandy/ympd project on windows
1. Install libmpdclient
I installed the libmpdclient-2.13 library from https://www.musicpd.org/libs/libmpdclient/
i did the steps to build the libmpdclient from the github repo
https://github.com/MusicPlayerDaemon/libmpdclient
This works well and got the following output with the .dll file:
2. Build the ympd project
I created a folder /build and inside this folder i did
cmake ..
i got this errors:
C:\mpd\ympd\build>cmake ..
-- Building for: Visual Studio 15 2017
-- Selecting Windows SDK version 10.0.15063.0 to target Windows 10.0.16299.
-- The C compiler identification is MSVC 19.11.25508.2
-- The CXX compiler identification is MSVC 19.11.25508.2
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/VC/Tools/MSVC/14.11.25503/bin/Hostx86/x86/cl.exe
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/VC/Tools/MSVC/14.11.25503/bin/Hostx86/x86/cl.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/VC/Tools/MSVC/14.11.25503/bin/Hostx86/x86/cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/VC/Tools/MSVC/14.11.25503/bin/Hostx86/x86/cl.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found PkgConfig: C:/MinGW/bin/pkg-config.exe (found version "0.26")
CMake Error at G:/Programme/CMake/share/cmake-3.10/Modules/FindPackageHandleStandardArgs.cmake:137 (message):
Could NOT find LibMPDClient (missing: LIBMPDCLIENT_LIBRARY
LIBMPDCLIENT_INCLUDE_DIR)
Call Stack (most recent call first):
G:/Programme/CMake/share/cmake-3.10/Modules/FindPackageHandleStandardArgs.cmake:378 (_FPHSA_FAILURE_MESSAGE)
cmake/FindLibMPDClient.cmake:27 (find_package_handle_standard_args)
CMakeLists.txt:20 (find_package)
-- Configuring incomplete, errors occurred!
See also "C:/mpd/ympd/build/CMakeFiles/CMakeOutput.log".
Question:
How can i give cmake the parameters LIBMPDCLIENT_LIBRARY LIBMPDCLIENT_INCLUDE_DIR?
Project Structure:
mpd/ympd/
build/
cmake/
FindLibMPDClient.cmake
contrib/
htdocs/
libmpdclient-2.13/
.output/
libmpdclient-2.dll
src/
tools/
.travis
CMakeLlists
LICENSE
README
ympd.1
File FindLibMPDClient.cmake:
# - Try to find LibMPDClient
# Once done, this will define
#
# LIBMPDCLIENT_FOUND - System has LibMPDClient
# LIBMPDCLIENT_INCLUDE_DIRS - The LibMPDClient include directories
# LIBMPDCLIENT_LIBRARIES - The libraries needed to use LibMPDClient
# LIBMPDCLIENT_DEFINITIONS - Compiler switches required for using LibMPDClient
find_package(PkgConfig)
pkg_check_modules(PC_LIBMPDCLIENT QUIET libmpdclient)
set(LIBMPDCLIENT_DEFINITIONS ${PC_LIBMPDCLIENT_CFLAGS_OTHER})
find_path(LIBMPDCLIENT_INCLUDE_DIR
NAMES mpd/player.h
HINTS ${PC_LIBMPDCLIENT_INCLUDEDIR} ${PC_LIBMPDCLIENT_INCLUDE_DIRS}
)
find_library(LIBMPDCLIENT_LIBRARY
NAMES mpdclient
HINTS ${PC_LIBMPDCLIENT_LIBDIR} ${PC_LIBMPDCLIENT_LIBRARY_DIRS}
)
set(LIBMPDCLIENT_LIBRARIES ${LIBMPDCLIENT_LIBRARY})
set(LIBMPDCLIENT_INCLUDE_DIRS ${LIBMPDCLIENT_INCLUDE_DIR})
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(LibMPDClient DEFAULT_MSG
LIBMPDCLIENT_LIBRARY LIBMPDCLIENT_INCLUDE_DIR
)
mark_as_advanced(LIBMPDCLIENT_LIBRARY LIBMPDCLIENT_INCLUDE_DIR)
every help is appropriate, thanks

CMAKE ERROR with CLion

I am a beginner in programming with C and I get this error code if i start a project on CLion:
C:\Program Files\JetBrains\CLion 2017.2.2\bin\cmake\bin\cmake.exe" -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - MinGW Makefiles" C:\Users\danie\CLionProjects\untitled2
-- The C compiler identification is GNU 5.3.0
-- The CXX compiler identification is unknown
-- Check for working C compiler: C:/MinGW/bin/gcc.exe
-- Check for working C compiler: C:/MinGW/bin/gcc.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
CMake Error at CMakeLists.txt:2 (project):
The CMAKE_CXX_COMPILER:
g++.exe
is not a full path and was not found in the PATH.
Tell CMake where to find the compiler by setting either the environment
variable "CXX" or the CMake cache entry CMAKE_CXX_COMPILER to the full path
to the compiler, or to the compiler name if it is in the PATH.
-- Configuring incomplete, errors occurred!
See also "C:/Users/danie/CLionProjects/untitled2/cmake-build-debug/CMakeFiles/CMakeOutput.log".
See also "C:/Users/danie/CLionProjects/untitled2/cmake-build-debug/CMakeFiles/CMakeError.log".
[Finished]
What can I do to get it working?
Thank you for your help.
By default, CMake expects that project needs support for both C and C++. If your project needs only C, specify that in project() call:
project(<project-name> C)

Cmake add library to a custom gcc compiler

I'm trying to compile a small program written in c with additional libraries zlib and libpng for an arm processor using using gcc-linaro-arm-linux.
In some .c file:
#include <bzlib.h>
If I compile with an ordinary gcc, everything is working fine, but with a gcc-linaro-arm-linux compiler it could not find libraries:
cmake:
$cmake -D CMAKE_C_COMPILER="${HOME}/opt/gcc-linaro-arm-linux-gnueabihf-4.8-2014.03_linux/bin/arm-linux-gnueabihf-gcc" -D CMAKE_CXX_COMPILER="${HOME}/opt/gcc-linaro-arm-linux-gnueabihf-4.8-2014.03_linux/bin/arm-linux-gnueabihf-g++" CMakeLists.txt
-- The C compiler identification is GNU 4.8.3
-- The CXX compiler identification is GNU 4.8.3
-- Check for working C compiler: /home/alex/opt/gcc-linaro-arm-linux-gnueabihf-4.8-2014.03_linux/bin/arm-linux-gnueabihf-gcc
-- Check for working C compiler: /home/alex/opt/gcc-linaro-arm-linux-gnueabihf-4.8-2014.03_linux/bin/arm-linux-gnueabihf-gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /home/alex/opt/gcc-linaro-arm-linux-gnueabihf-4.8-2014.03_linux/bin/arm-linux-gnueabihf-g++
-- Check for working CXX compiler: /home/alex/opt/gcc-linaro-arm-linux-gnueabihf-4.8-2014.03_linux/bin/arm-linux-gnueabihf-g++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Could NOT find ZLIB (missing: ZLIB_LIBRARY) (found version "1.2.8")
CMake Error at /usr/share/cmake-2.8/Modules/FindPackageHandleStandardArgs.cmake:108 (message):
Could NOT find PNG (missing: PNG_LIBRARY PNG_PNG_INCLUDE_DIR)
Call Stack (most recent call first):
/usr/share/cmake-2.8/Modules/FindPackageHandleStandardArgs.cmake:315 (_FPHSA_FAILURE_MESSAGE)
/usr/share/cmake-2.8/Modules/FindPNG.cmake:105 (find_package_handle_standard_args)
src/CMakeLists.txt:3 (find_package)
You need to tell CMake where to find your target sysroot. Check out CMAKE_FIND_ROOT_PATH.

cmake doesn't display display message

Fedora 15
cmake version 2.8.4
I am using the following CMakeLists.txt. However the status message doesn't display when I run cmake .
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
PROJECT(proj2 C)
IF(CMAKE_COMPILER_IS_GNUCXX)
MESSAGE(STATUS "==== GCC detected - Adding compiler flags")
SET(CMAKE_C_FLAGS "-pthread -ggdb -Wextra -Wall")
ENDIF(CMAKE_COMPILER_IS_GNUCXX)
ADD_EXECUTABLE(crypto_app main.c)
TARGET_LINK_LIBRARIES(crypto_app crypt)
All I get is the following:
-- The C compiler identification is GNU
-- Check for working C compiler: /usr/lib64/ccache/gcc
-- Check for working C compiler: /usr/lib64/ccache/gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/projects/proj1/
Many thanks for any suggestions about this.
You're telling cmake that it's a C project, and then checking for a CXX (i.e. C++) compiler. CMAKE_COMPILER_IS_GNUCXX will never be true in this case. That's why.

Resources