Covert gcc arguments to CMake file - c++

So I am trying to learn CMake and I want to convert the following
gcc "-DARCH=\"`uname -a`\"" cli_arch.c arch.c
to a CMakeLists.txt file.
This is what I got so far:
Running cmake -DCMAKE_C_COMPILER=/usr/bin/gcc "-DARCH=\"`uname -a`\"" ..
// CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
project(cli_arch)
add_executable(cli_arch cli_arch.c arch.c)
The result I am getting is:
-- The C compiler identification is AppleClang 11.0.3.11030032
-- The CXX compiler identification is AppleClang 11.0.3.11030032
-- Check for working C compiler: /usr/bin/gcc
-- Check for working C compiler: /usr/bin/gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
CMake Warning:
Manually-specified variables were not used by the project:
ARCH
I am not understanding how to pass the arguments "-DARCH=\"`uname -a`\"" to CMake.

The trick is execute_process.
execute_process(COMMAND uname -s
OUTPUT_VARIABLE uname_output
OUTPUT_STRIP_TRAILING_WHITESPACE
)
add_executable(cli_arch cli_arch.c arch.c)
target_compile_definitions(cli_arch
PRIVATE "-DARCH=\"${uname_output}\""
)
I had to use uname -s since at least on my system, uname -a has a # which gcc refuses to accept in a macro via the command line.
Edit, since you changed your question. You're passing ARCH to cmake but not doing anything will it. You can pass the argument along using the same target_compile_definitions line I have above.
$ cmake -DARCH2=foo .
$ make
$ ./a
Linux
foo
The relevant part of the new CMakeLists.txt looks like this:
target_compile_definitions(a
PRIVATE "-DARCH=\"${uname_output}\""
PRIVATE "-DARCH2=\"${ARCH2}\""
)

Related

Cmake automatically defaults to gcc-5

I was trying to compile amber tools using cmake. I am using ubuntu Ubuntu 16.04.7 LTS with the default gcc version gcc-5 and g++5, and amber tool requires at least gcc-6. I have installed gcc-8 and g++8 and specified it using the CMAKE_C_COMPILER and CMAKE_CXX_COMPILER. The cmake command used is the command below:
cmake $AMBER_PREFIX/amber22_src \
-DCMAKE_INSTALL_PREFIX=$AMBER_PREFIX/amber22 \
-DCOMPILER=GNU \
-DMPI=FALSE -DCUDA=TRUE -DINSTALL_TESTS=TRUE \
-DDOWNLOAD_MINICONDA=TRUE \
-DCMAKE_C_COMPILER=/usr/bin/gcc-8 -DCMAKE_CXX_COMPILER=/usr/bin/g++-8 \
Even though the gcc g++ is specified I get the following error:
-- Starting configuration of Amber version 22.0.0...
-- CMake Version: 3.14.0
-- For how to use this build system, please read this wiki:
-- http://ambermd.org/pmwiki/pmwiki.php/Main/CMake
-- For a list of important CMake variables, check here:
-- http://ambermd.org/pmwiki/pmwiki.php/Main/CMake-Common-Options
-- **************************************************************************
-- Setting C compiler to gcc
-- Setting CXX compiler to g++
-- Setting Fortran compiler to gfortran
-- Amber source not found, only building AmberTools
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- The Fortran compiler identification is GNU 5.4.0
-- Check for working C compiler: /usr/bin/gcc
-- Check for working C compiler: /usr/bin/gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/g++
-- Check for working CXX compiler: /usr/bin/g++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Check for working Fortran compiler: /usr/bin/gfortran
-- Check for working Fortran compiler: /usr/bin/gfortran -- works
-- Detecting Fortran compiler ABI info
-- Detecting Fortran compiler ABI info - done
-- Checking whether /usr/bin/gfortran supports Fortran 90
-- Checking whether /usr/bin/gfortran supports Fortran 90 -- yes
--
************************************************************
Error: Amber requires at least g++-6.0
See https://ambermd.org/Installation.php for more info
************************************************************
--
CMake Error at cmake/VerifyCompilerConfig.cmake:30 (message):
Call Stack (most recent call first):
cmake/AmberBuildSystem2ndInit.cmake:30 (include)
CMakeLists.txt:111 (include)
-- Configuring incomplete, errors occurred!
See also "/media/gpu-1/GPU_1_2TB/Ambertools22/amber22_src/build/CMakeFiles/CMakeOutput.log".
You have changed variables that require your cache to be deleted.
Configure will be re-run and you may have to reset some variables.
The following variables have changed:
CMAKE_C_COMPILER= /usr/bin/gcc-8
CMAKE_CXX_COMPILER= /usr/bin/g++-8
-- Generating done
CMake Warning:
Manually-specified variables were not used by the project:
DOWNLOAD_MINICONDA
MPI
-- Build files have been written to: /media/gpu-1/GPU_1_2TB/Ambertools22/amber22_src/build
If the cmake build report looks OK, you should now do the following:
make install
source /media/gpu-1/GPU_1_2TB/Ambertools22/amber22/amber.sh
Thank you
I would not call it a problem of CMake, rather on the way Amber performs the control of the compiler through CMake, which most likely is performed by taking the first compiler that appears from the environment variable $PATH.
If you would like to compile it anyway, I can suggest two cheap ways:
using the update-alternatives package, to change the "system
default" gcc and g++
prepend a path in PATH with export PATH=absolute/path/to/a/folder:$PATH, and in this path put two symbolic links called gcc and g++, that points at
gcc-8 and g++-8
According to this, you are still using existing cache files that you've generated before you set the new C and CXX compiler.
You have changed variables that require your cache to be deleted.
Configure will be re-run and you may have to reset some variables.
The following variables have changed:
CMAKE_C_COMPILER= /usr/bin/gcc-8
CMAKE_CXX_COMPILER= /usr/bin/g++-8
Based on cmake $AMBER_PREFIX/amber22_src you are running this from your build folder. In general I recommend using the syntax cmake -S[source] -B[build]. Your current working directory should be completely deleted. Once you do that, re-run your cmake command and it should be correctly set.
If it doesn't work you can do a sanity check by adding
set(CMAKE_C_COMPILER [path_to_C])
set(CMAKE_CXX_COMPILER [path_to_CXX])
BE SURE TO PUT IT AT THE START OF THE CMAKELISTS FILE i.e. before any project() line.
EDIT: Also have you heard about docker? It's a great tool for creating special contained environments for using legacy tools.

Can't get CLion to actually use GCC compiler instead of Clang?

I've tried following all the help to change my compiler to use GCC instead of Clang (on macOS) as I would like to try to use __gnu_pbds::priority_queue instead of the STL version, and it still appears that CMake uses Clang. I've made a toolchain that looks like this:
and set it to be used in the CMake profile.
However, it appears CMake skips actually using the compiler - here is the CMake output:
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER=/usr/bin/gcc -DCMAKE_CXX_COMPILER=/usr/bin/g++ -G "CodeBlocks - Unix Makefiles" -S /Users/steve/CLionProjects/TestModernTimeStretch -B /Users/steve/CLionProjects/TestModernTimeStretch/cmake-build-release-gcc
-- The C compiler identification is AppleClang 13.1.6.13160021
-- The CXX compiler identification is AppleClang 13.1.6.13160021
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/gcc - 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/g++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for stdbool.h
-- Looking for stdbool.h - found
-- Looking for unistd.h
-- Looking for unistd.h - found
-- Checking processor clipping capabilities...
-- Performing Test CPU_CLIPS_POSITIVE
-- Performing Test CPU_CLIPS_POSITIVE - Failed
-- Performing Test CPU_CLIPS_NEGATIVE
-- Performing Test CPU_CLIPS_NEGATIVE - Failed
-- Checking processor clipping capabilities... none
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/steve/CLionProjects/TestModernTimeStretch/cmake-build-release-gcc

Cmake Error Compiling Hydrogen 0.9.7 Beta1

I am attempting to compile Hydrogen 0.9.7 beta 2 with Cmake and I keep getting a cmakelists.txt error: CMake Error at CMakeLists.txt:139 (MANDATORY_PKG):
Unknown CMake command "MANDATORY_PKG"
Reviewing the list of Cmake commands I cant find a command that corresponds to MANDATORY_PKG. Is this deprecated language?
Here is the output of the attempted build.
tinman13#tinman13-scythion:~/hydrogen-1.0.0-beta1/build$ sudo cmake /home/tinman13 /hydrogen-1.0.0-beta1/build
-- The C compiler identification is GNU 9.3.0
-- The CXX compiler identification is GNU 9.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
fatal: not a git repository (or any of the parent directories): .git
CMake Error at CMakeLists.txt:138 (INCLUDE):
INCLUDE could not find load file:
StatusSupportOptions
CMake Error at CMakeLists.txt:139 (MANDATORY_PKG):
Unknown CMake command "MANDATORY_PKG".
-- Configuring incomplete, errors occurred!
See also "/home/tinman13/hydrogen-1.0.0-beta1/build/CMakeFiles/CMakeOutput.log".
CMakeOutput.log is a large rambling file but i'll post it if you like.
Is there anything that can be done to rectify this? Can i switch to an older version of CMake? Googling produces very little on the topic of unknown CMake commands and nothing related to Hydrogen. Any help would be greatly appreciated.

Make command not producing an executable file

I just started using C++ and want to use CMake to compile a simple hello world program, but I don't know what to do with the MakeFile once it is produced. I have a directory called tutorial that contains my tutorial.cpp file containing the hello world code. I also have a CMakeLists file that contains:
cmake_minimum_required(VERSION 3.14.4)
project (tutorial)
add_executable(tutorial tutorial.cpp)
When I run the command cmake . from the tutorial directory in my terminal, all the correct (at least I think they are correct) files are produced and I get the following output:
-- The C compiler identification is AppleClang 10.0.1.10010046
-- The CXX compiler identification is AppleClang 10.0.1.10010046
-- Check for working C compiler:
/Library/Developer/CommandLineTools/usr/bin/cc
-- Check for working C compiler:
/Library/Developer/CommandLineTools/usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler:
/Library/Developer/CommandLineTools/usr/bin/c++
-- Check for working CXX compiler:
/Library/Developer/CommandLineTools/usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to:
/Users/kshitijsachan/Documents/cs/cpp/tutorial
I then typed in make in the terminal and got no output. How can I actually run the makefile that was produced?
Run make <some_valid_target_name> - like make all or make install (if such commonly used targets exist) or whatever other targets your project defined. Check your projects documentation or CMakeLists.txt file for valid target names.
See also cmake_add_executable which adds a target.

CMake with mingw on Windows 10: Detecting CXX compiler ABI info - failed

I want to use cmake to create MinGW Makefiles on windows 10. I have installed mingw and I can use mingw32-make and g++ commands without issue.
The test project is a super simple CMakeLists.txt file:
cmake_minimum_required(VERSION 3.10)
project(hello-world)
add_executable(hello-world main.cpp)
and a simple main.cpp file:
#include <iostream>
int main() {
std::cout << "hello Visual Studio Code! :)" << '\n';
return 0;
}
These are the commands I use to create the makefiles:
>> mkdir build
>> cd build
>> cmake -G "MinGW Makefiles" ..
This is where I get errors:
CMake Error at C:/Program Files (x86)/CMake/share/cmake-3.10/Modules/CMakeDetermineCompilerId.cmake:495 (file):
file STRINGS file
"C:/Projects/test/build/CMakeFiles/3.10.1/CompilerIdCXX/a.exe" cannot be read.
... some more errors in the test files
-- The CXX compiler identification is GNU 6.3.0
-- 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
-- Check for working CXX compiler: C:/MinGW/bin/g++.exe
-- Check for working CXX compiler: C:/MinGW/bin/g++.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - failed
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring incomplete, errors occurred!
This is from the error log:
The CXX compiler identification could not be found in "C:/Projects/test/build/CMakeFiles/3.10.1/CompilerIdCXX/a.exe"
What is the CXX compiler ABI info and how can it be, that there is a working CXX compiler detected but this fails?
Let me know if you need anymore info about the setup!
What turned out to be my culprit was Windows Defender! Turned that off and the CXX compiler ABI info - failed issue went away.
I used the CMake-GUI tool to figure out the missing tool dependencies. For example : CMAKE_ADDR2LINE was not set, once I pointed it to the correct location of addr2line.exe ,the errors were resolved.