cMake to compile C source files with g++ compiler - c

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

Related

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

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.

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)

How to change compiler from C to C++ in Eclipse IDE?

My project is in C and shared library in C++ (I using Eclipse IDE on Linux platform). Default setting of project taking C compiler (GCC). Can anybody suggest me how to change compiler from C to C++ for my project.
GCC can compile both C and C++ source files. It uses filename extensions to determine if it should compile as C or C++.
C++ source files conventionally use one of the suffixes ‘.C’, ‘.cc’, ‘.cpp’, ‘.CPP’, ‘.c++’, ‘.cp’, or ‘.cxx’; C++ header files often use ‘.hh’, ‘.hpp’, ‘.H’, or (for shared template code) ‘.tcc’; and preprocessed C++ files use the suffix ‘.ii’. GCC recognizes files with these names and compiles them as C++ programs even if you call the compiler the same way as for compiling C programs (usually with the name gcc).
GCC: GNU Compiler Collection
gcc: GNU C Compiler g++: GNU C++ Compiler
The main differences:
gcc will compile: .c/.cpp files as C and C++ respectively.
g++ will compile: .c/.cpp files but they will all be treated as C++ files.
Also if you use g++ to link the object files it automatically links in the std C++ libraries (gcc does not do this).

How do I set gcc to use the file extension (.c or .cpp) to determine the correct compiler/linker?

I have a simple, representative C program, stored in a file called hello.c:
#include <stdio.h>
int main(void)
{
printf('Hello, world\n');
return 0;
}
On my Linux machine, I attempted to compile the program with gcc:
gcc hello.c
which returns an error:
undefined reference to "___gxx_personality_v0" ... etc
As has been discussed before in the context of C++, this problem arises in the linking stage, when gcc attempts to link C libraries to a C++ program, thus giving the error. In one of the answers, someone mentioned that the extension does matter, and that gcc requires the .c extension when compiling C files, and some other extension (e.g. .cpp) when compiling C++ files.
Question: How do I set gcc to use the file extension to determine which compiler to use, since gcc seems to be defaulting to C++ on my system? Specifying the language through the file extension alone doesn't seem to be enough. If I specify the language using the -x flag, gcc functions as expected.
gcc -x c hello.c
Typically, you let make decide this.
GNU Make has built in implicit rules, which automatically pick the right compiler.
Try a Makefile with these contents:
all: some_file.o some_other_file.o
And then place a some_file.cpp and some_other_file.c in the same directory, and gnu make will automatically pick the correct compiler. The linker, you may still have to provide yourself. When mixing C and C++, it's usually easiest to link with g++, like so:
program.exe: some_file.o some_other_file.o
g++ -o $# #^
This is the same as:
program.exe: some_file.o some_other_file.o
g++ -o program.exe some_file.o some_other_file.o

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