C++
std::string
is a dynamic,
contiguous
container for character strings.
String data is easily and efficiently passed between std::string to / from a C or Fortran function that expects a char* pointer.
The basic algorithm is:
allocate std::string with desired size and fill with \0.
use std::string::data() to get a char* pointer to the string data that is read/write for the C or Fortran function (or C++).
use std::string::c_str() to get a const char* pointer to the string data that is read-only for the C or Fortran function (or C++). This trims the string to the first \0 character. Otherwise, the std::string::length() will include all the unwanted trailing \0 characters.
The
CMake Snap package
allows easy install of the latest CMake version.
Scroll down to the “Install CMake on your Linux distribution” section and click on the distribution closest to the computer being used to ensure Snap is setup correctly.
After CMake install, add to PATH in ~/.profile or similar like:
The #if strictly check that the compiler language support is at least the specified version.
Most compilers have long-supported the #warning directive without the #if check needed.
That is, the following is sufficient for most compilers:
C++
std::make_unique()
is a C++14 feature that creates std::unique_ptr without using
new
and delete.
std::make_unique() is a safer and more convenient way to manage memory in C++.
GCC 4.9
circa 2014 added support for std::make_unique(), along with virtually all modern C++ compilers for several years already.
__cpp_lib_make_unique
feature test macro can fence non-essential code if supporting ancient compilers is required.
CMake target property
TEST_LAUNCHER
allows specifying a test launcher program.
For example, MPI programs can use mpiexec to run tests with parameters.
This allows deduplicating or making more programmatic test runner scripts.
Typically we create a small CMake function to configure the test launcher for each target and the associated tests.
Fortran compilers typically use 4 bytes for
logical
while C compilers usually use
1 byte for bool.
oneAPI flag
-fpscomp logicals
ensures integer values 0,1 corresponding to .false., .true. are used.
Otherwise by default unexpected values may cause programs to fail at runtime with impossible boolean values like 255.
The Intel oneAPI compiler
-standard-semantics
flag to use C_BOOL
values
correctly between C, C++ and Fortran has a BIG DOWNSIDE in that it breaks linking with system libraries–including IntelMPI!
All projects and libraries linking to a Fortran library that used oneAPI -standard-semantics must also use -standard-semantics.
GCC 5
enabled the
__has_include
macro that checks if a source file exists.
__has_include() was made language standard syntax in C++17 and C23.
__has_include("file.h") checks if “file.h” exists as a file on the compiler includes paths, but not if “file.h” can be included with the specified compiler language standard.
Watch out for buggy __has_include() in GCC < 10.
We found that __has_include("linux/magic.h") with quotes "" instead of the usual carets __has_include(<linux/magic.h>) was needed specifically for
header linux/magic.h.
Other systems libraries with carets worked as expected even with GCC < 10.
#if __has_include("linux/magic.h") // quotes needed for GCC < 10
# include <linux/magic.h>
#endif
CMake FetchContent is useful to incorporate subprojects at configure time.
FetchContent subproject cache variables can override the top-level project cache, which can be confusing.
Projects may wish to use a default install prefix when
cmake –install-prefix
is not specified.
Environment variable
CMAKE_INSTALL_PREFIX
can set a default install prefix across projects.
Some projects force a default install prefix if not specified by the
top level project:
However, it is more a canonical approach to check the install prefix
write permissions.
message(STATUS"CMAKE_INSTALL_PREFIX: ${CMAKE_INSTALL_PREFIX}")file(MAKE_DIRECTORY${CMAKE_INSTALL_PREFIX})if(CMAKE_VERSIONVERSION_GREATER_EQUAL3.29) if(NOTIS_WRITABLE${CMAKE_INSTALL_PREFIX}) message(FATAL_ERROR"CMAKE_INSTALL_PREFIX is not writable: ${CMAKE_INSTALL_PREFIX}") endif()else() file(TOUCH${CMAKE_INSTALL_PREFIX}/.cmake_writable"")endif()