Is there a way to use the Clang compiler while mixing C++ and Fortran?
Until now I use cmake with
project(mixing CXX Fortran)
but this triggers the use of g++.
-- The CXX compiler identification is GNU 6.2.0
CMakeLists.txt of my project with Fortran mixing:
cmake_minimum_required(VERSION 3.7.0)
project(mixing CXX Fortran)
# SET UP ROOT https://root.cern.ch/how/integrate-root-my-project-cmake
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} /opt/local/libexec/root6/etc/root/cmake)
find_package(ROOT REQUIRED COMPONENTS MATH MINUIT2)
include(${ROOT_USE_FILE})
include_directories(Experiment Theory ${ROOT_INCLUDE_DIRS})
add_executable(mixing main.cpp)
target_link_libraries(mixing ${ROOT_LIBRARIES})
Not working, because g++ cannot use the needed Clang flag -stdlib=libc++ of the ROOT library.
You can always override c/c++ compiler by changing CMAKE_<LANG>_COMPILER, where <LANG> in your case is C or CXX.
You can set your CC and CXX environment variables to override defaults for CMAKE_C_COMPILER / CMAKE_CXX_COMPILER:
CC=clang CXX=clang++ cmake
You can set these on command line when invoking cmake:
cmake -D CMAKE_C_COMPILER=clang -D CMAKE_CXX_COMPILER=clang++
You can also set these directly in your cmake file:
set(CMAKE_C_COMPILER clang)
set(CMAKE_CXX_COMPILER clang++)
make sure however that you do that at the very top of your cmake file before any project/enable_language directive is used.
Related
I'm on win10 and I have installed: MSVS2022 with msc++ compiler, codeblocks with Mingwin-g++ compiler and then llvm clang.
I then installed latest cmake and have a very simple CMakeLists.txt in a directory named basic with some cc files:
cmake_minimum_required(VERSION 2.8)
project(my)
add_executable(my main.cc power.cc)
I do these commands in powershell:
mkdir build
cd build
cmake ../basic.
-- Building for: Visual Studio 17 2022
CMake Deprecation Warning at CMakeLists.txt:17 (cmake_minimum_required):
Compatibility with CMake < 2.8.12 will be removed from a future version of
CMake.
Update the VERSION argument <min> value or use a ...<max> suffix to tell
CMake that the project does not need compatibility with older versions.
-- The C compiler identification is MSVC 19.31.31104.0
-- The CXX compiler identification is MSVC 19.31.31104.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: d:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.31.31103/bin/H
But I wish I could use g++ or clang++, so I tried several methods at the same time.
(1) I changed CMakeLists.txt to add some lines:
set(CMAKE_C_COMPILER "clang")
set(CMAKE_CXX_COMPILER "clang++")
SET (CMAKE_C_COMPILER "d:\\Program Files\\LLVM\\bin\\clang.exe")
SET (CMAKE_C_FLAGS "-Wall -std=c99")
SET (CMAKE_C_FLAGS_DEBUG "-g")
SET (CMAKE_C_FLAGS_MINSIZEREL "-Os -DNDEBUG")
SET (CMAKE_C_FLAGS_RELEASE "-O4 -DNDEBUG")
SET (CMAKE_C_FLAGS_RELWITHDEBINFO "-O2 -g")
SET (CMAKE_CXX_COMPILER "d:\\Program Files\\LLVM\\bin\\clang++.exe")
SET (CMAKE_CXX_FLAGS "-Wall")
SET (CMAKE_CXX_FLAGS_DEBUG "-g")
SET (CMAKE_CXX_FLAGS_MINSIZEREL "-Os -DNDEBUG")
SET (CMAKE_CXX_FLAGS_RELEASE "-O4 -DNDEBUG")
SET (CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2 -g")
cmake_minimum_required(VERSION 2.8)
project(my)
add_executable(my main.cc power.cc)
I cleaned the directory under build and run cmake again. Doesn't work, still choose msvc compiler.
Then I set environmental variable in Powershell like this:
$env:CC="d:\Program Files\LLVM\bin\clang.exe"
$env:CXX="d:\Program Files\LLVM\bin\clang++.exe"
I tried again. Doesn' work. Then I used the command line option:
cmake ..\basic\ -DCMAKE_CXX_COMPILER="d:\Program Files\LLVM\bin\clang++.exe"
Doesn't work. All methods tried together, still chooses msvc.
How to solve this problem?
Trying to build samples/mqtt/basic_pub_sub sample from aws-iot-device-sdk-cpp-v2 which contains CMakeList.txt:
cmake_minimum_required(VERSION 3.1)
# note: cxx-17 requires cmake 3.8, cxx-20 requires cmake 3.12
project(basic-pub-sub CXX)
file(GLOB SRC_FILES
"*.cpp"
)
add_executable(${PROJECT_NAME} ${SRC_FILES})
set_target_properties(${PROJECT_NAME} PROPERTIES
CXX_STANDARD 14)
#set warnings
if (MSVC)
target_compile_options(${PROJECT_NAME} PRIVATE /W4 /WX /wd4068)
else ()
target_compile_options(${PROJECT_NAME} PRIVATE -Wall -Wno-long-long -pedantic -Werror)
endif ()
find_package(aws-crt-cpp REQUIRED)
target_link_libraries(${PROJECT_NAME} AWS::aws-crt-cpp)
Trying to build:
mkdir build
cd build
cmake ..
Got errro:
-- The CXX compiler identification is GNU 9.3.0
-- 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
CMake Error at CMakeLists.txt:21 (find_package):
By not providing "Findaws-crt-cpp.cmake" in CMAKE_MODULE_PATH this project
has asked CMake to find a package configuration file provided by
"aws-crt-cpp", but CMake did not find one.
Could not find a package configuration file provided by "aws-crt-cpp" with
any of the following names:
aws-crt-cppConfig.cmake
aws-crt-cpp-config.cmake
Add the installation prefix of "aws-crt-cpp" to CMAKE_PREFIX_PATH or set
"aws-crt-cpp_DIR" to a directory containing one of the above files. If
"aws-crt-cpp" provides a separate development package or SDK, be sure it
has been installed.
-- Configuring incomplete, errors occurred!
I found aws-crt-cpp-config.cmake if folder home\a\cpp_projects\sdk-cpp-workspace\lib\aws-crt-cpp\cmake
But how to tell about it to CMake?
I've been trying to change a makefile c++ project into a cmake project, and I've been having som difficulty. cmake seems to be looking for stuff in /usr/local/lib/ instead of /usr/local/include/ and I'm not sure why that is.
This library is header-only, and so I've been following this tutorial My header-only library in include seems to "build" fine, but I keep getting the following error when I try to generate a makefile to build my example program:
me:~/pf/examples/build$ cmake ..
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.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
CMake Error at /usr/local/lib/cmake/pf/pfConfig.cmake:27 (include):
include could not find load file:
/usr/local/lib/cmake/pf/pf_exampleTargets.cmake
Call Stack (most recent call first):
CMakeLists.txt:15 (find_package)
examples/CMakeLists.txt creates another fresh project:
project(pf_example)
cmake_minimum_required (VERSION 3.12)
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()
set(CMAKE_CXX_FLAGS "-Wall -Wextra")
set(CMAKE_CXX_FLAGS_DEBUG "-g")
set(CMAKE_CXX_FLAGS_RELEASE "-O3")
set(CMAKE_CXX_STANDARD 17)
# "install" pf
find_package(pf CONFIG REQUIRED)
find_package(Eigen3 3.3 REQUIRED NO_MODULE)
file(GLOB SOURCES ${PROJECT_NAME}/*.{h,cpp})
message("${SOURCES}")
add_executable(${PROJECT_NAME} ${SOURCES})
target_link_libraries(${PROJECT_NAME} pf::pf)
The root directory CMakeLists.txt file is more complicated. It's the one that was adapted from the tutorial I mentioned above:
cmake_minimum_required(VERSION 3.12)
project("pf" VERSION 1.0.1
DESCRIPTION "A header only c++ template library for fast particle filtering."
HOMEPAGE_URL "https://github.com/tbrown122387/pf")
include(GNUInstallDirs)
add_library(${PROJECT_NAME} INTERFACE)
target_include_directories(
${PROJECT_NAME}
INTERFACE $<BUILD_INTERFACE:${${PROJECT_NAME}_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
target_compile_features(${PROJECT_NAME} INTERFACE cxx_std_17)
install(TARGETS ${PROJECT_NAME}
EXPORT ${PROJECT_NAME}_Targets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
include(CMakePackageConfigHelpers)
write_basic_package_version_file("${PROJECT_NAME}ConfigVersion.cmake"
VERSION ${PROJECT_VERSION}
COMPATIBILITY SameMajorVersion)
configure_package_config_file(
"${PROJECT_SOURCE_DIR}/cmake/${PROJECT_NAME}Config.cmake.in"
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
INSTALL_DESTINATION
${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake)
install(EXPORT ${PROJECT_NAME}_Targets
FILE ${PROJECT_NAME}Targets.cmake
NAMESPACE ${PROJECT_NAME}::
DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake)
install(FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake)
install(DIRECTORY ${PROJECT_SOURCE_DIR}/include DESTINATION include)
To install this header only library, before I try to build the examples project, I typed the following commands into the command line:
cd ~/pf
mkdir build
cd build/
cmake .. -DCMAKE_INSTALL_PREFIX:PATH=/usr/local/include
sudo cmake --build . --config Release --target install -- -j $(nproc)
I didn't post it, but there's also a file cmake/pfconfig.cmake.in that is verbatim copied from the tutorial above.
Your install prefix is specified as /usr/local/include so the files would be installed as:
headers into /usr/local/include/include
libraries into /usr/local/include/libs
cmake stuff into /usr/local/include/share/${PROJECT_NAME}/cmake
Those paths are just wrong. Just set CMAKE_INSTALL_PREFIX=/usr/local (ie. remove include) and install it inside /usr/local/ tree.
A few took care of this issue.
in CMakeLists.txt change install(DIRECTORY ${PROJECT_SOURCE_DIR}/include DESTINATION include) to install(DIRECTORY ${PROJECT_SOURCE_DIR}/include/pf DESTINATION include) so the files don't clutter up the installation locations. This also requires creating pf/include/pf and moving all the files in pf/include to pf/include/pf.
Follow advice in #KamilCuk's answer.
In examples/CMakeLists.txt change file(GLOB SOURCES ${PROJECT_NAME}/*.{h,cpp}) to file(GLOB SOURCES ${CMAKE_SOURCE_DIR}/*.h ${CMAKE_SOURCE_DIR}/*.cpp)
Also note that my /usr/local/lib and /usr/local/include were quite cluttered up due to my numerous earlier failed attempts, so I deleted a bunch of files in there and re-installed fresh.
I am trying to create a Eclipse C++ project by CMake which calls torch/torch.h . I run cmake -G "Eclipse CDT4 - Unix Makefiles" ./ to create a Eclipse project, but I get this error:
-- The C compiler identification is GNU 7.3.0
-- The CXX compiler identification is GNU 7.3.0
-- Could not determine Eclipse version, assuming at least 3.6 (Helios). Adjust CMAKE_ECLIPSE_VERSION if this is wrong.
-- 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
CMake Error at CMakeLists.txt:10 (find_package):
By not providing "FindTorch.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "Torch", but
CMake did not find one.
Could not find a package configuration file provided by "Torch" with any of
the following names:
TorchConfig.cmake
torch-config.cmake
Add the installation prefix of "Torch" to CMAKE_PREFIX_PATH or set
"Torch_DIR" to a directory containing one of the above files. If "Torch"
provides a separate development package or SDK, be sure it has been
installed.
-- Configuring incomplete, errors occurred!
In which CMakeLists.txt is located in the current directory that has:
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(test)
find_package(Torch REQUIRED)
add_executable(test test.cpp)
target_link_libraries(test "${TORCH_LIBRARIES}")
set_property(TARGET test PROPERTY CXX_STANDARD 11)
Apparently, it cannot find TorchConfig.cmake and torch-config.cmake files; although, I have TorchConfig.cmake in /home/afshin/libtorch/share/cmake/Torch. I added the corresponding path into the CMakeLists.txt file by testing each of:
set(CMAKE_MODULE_PATH "/home/afshin/libtorch/share/cmake/Torch;${CMAKE_MODULE_PATH}")
set(CMAKE_MODULE_PATH "/home/afshin/libtorch/share/cmake;${CMAKE_MODULE_PATH}")
set(CMAKE_MODULE_PATH "/home/afshin/libtorch;${CMAKE_MODULE_PATH}")
set(Torch_DIR "/home/afshin/libtorch;${Torch_DIR}")
set(Torch_DIR "/home/afshin/libtorch/share/cmake/Torch;${Torch_DIR}")
set(Torch_DIR "/home/afshin/libtorch/share/cmake;${Torch_DIR}")
set(DCMAKE_PREFIX_PATH "/home/afshin/libtorch/share/cmake/Torch;${DCMAKE_PREFIX_PATH}")
set(DCMAKE_PREFIX_PATH "/home/afshin/libtorch/share/cmake;${DCMAKE_PREFIX_PATH}")
set(DCMAKE_PREFIX_PATH "/home/afshin/libtorch;${DCMAKE_PREFIX_PATH}")
But, it did not help and I still get same error.
I appreciate any help or comments.
I also tried the cmake-gui and I get same error:
Thanks,
Afshin
I was able to solve the problem by editing the CMakeLists.txt as:
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(test_cmake)
set(CMAKE_PREFIX_PATH "/home/afshin/libtorch/share/cmake/Torch")
find_package(Torch REQUIRED)
add_executable(test_cmake ./src/test_cmake.cpp)
target_link_libraries(test_cmake "${TORCH_LIBRARIES}")
set_property(TARGET test_cmake PROPERTY CXX_STANDARD 11)
Alternatively, using the cmake-gui also I was able to solve the problem by the following setting:
by hitting configure, and then generate.
Finally, I imported this project into Eclipse by selecting Makefile Project With Existing Code. This code is compiled and built successfully.
The following modified CMakeLists.txt file works without the apparently missing TorchConfig.cmake (also missing in the vcpkg installation here). I recommend Microsoft's vcpkg for cross-platform packages (c++ libraries) management (usage here). But the code is vcpkg independent: one can set TORCH_BASE_PATH (or Torch_INCLUDE_DIR and Torch_LIBRARIES) to the proper paths.
#[[
tested with:
- CMake 3.13
- Visual Studio Community Edition 15.9.4
(CMake generator: "Visual Studio 15 2017 Win64")
- torch-th library installed with vcpkg
generic: vcpkg install torch-th
for macOS: vcpkg install torch-th:x64-osx-dynamic
x64-osx-dynamic triplet must be created: x64-osx + "set(VCPKG_LIBRARY_LINKAGE dynamic)"
for Windows: vcpkg install torch-th:x64-windows
- easy torch sample: https://apaszke.github.io/torch-internals.html
]]
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(test)
# cannot work without a "package configuration file" (TorchConfig.cmake)
# so we replace it with find_path and find_library
#find_package(Torch REQUIRED)
#[[
the environement variable VCPKG_ROOT used here, contains the path to vcpkg installation folder
replace the two paths with your paths to TH installation
usage: #include "TH/TH.h"
]]
set(TORCH_BASE_PATH "$ENV{VCPKG_ROOT}/installed/${VCPKG_TARGET_TRIPLET}")
message(STATUS TORCH_BASE_PATH=${TORCH_BASE_PATH})
set(Torch_INCLUDE_DIR "${TORCH_BASE_PATH}/include")
set(Torch_LIBRARIES "${TORCH_BASE_PATH}/lib")
# target_link_libraries is to be preferred
#link_directories(${Torch_LIBRARIES})
find_library(LIBRARY_TORCH TH HINTS ${Torch_LIBRARIES})
#[[
even simpler
if you use the vcpkg toolchain file "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake"
find_path(Torch_INCLUDE_DIR TH/TH.h)
find_library(LIBRARY_TORCH TH)
]]
add_executable(test test.cpp)
target_include_directories(test PRIVATE ${Torch_INCLUDE_DIR})
target_link_libraries(test ${LIBRARY_TORCH})
set(CMAKE_CXX_STANDARD 11)
#set_property(TARGET test PROPERTY CXX_STANDARD 11)
I am trying to setup cmake using Windows 10 using MinGW. I have included the path c:/MinGW/bin in my system path and environment path settings. I have removed sh.exe from my path (although, i would love to be able to keep this if possible).
CMakeLists.txt
cmake_minimum_required(VERSION 3.8)
set(CMAKE_C_COMPILER "C:/MinGW/bin/gcc.exe")
set(CMAKE_CXX_COMPILER "C:/MinGW/bin/gcc.exe")
project (Tutorial)
add_executable(Tutorial tutorial.cpp)
Output
C:\School\athabascua\data structures\ass1>cmake -g "MinGW Makefiles" .
-- The C compiler identification is GNU 5.3.0
-- The CXX compiler identification is GNU 5.3.0
-- Check for working C compiler: C:/MinGW/bin/gcc.exe
CMake Error: Generator: execution of make failed. Make command was: "nmake" "/NOLOGO" "cmTC_b3144\fast"
-- Check for working C compiler: C:/MinGW/bin/gcc.exe -- broken
CMake Error at C:/Program Files/CMake/share/cmake-3.8/Modules/CMakeTestCCompiler.cmake:51 (message):
The C compiler "C:/MinGW/bin/gcc.exe" is not able to compile a simple test
program.
It fails with the following output:
Change Dir: C:/School/athabascua/data structures/ass1/CMakeFiles/CMakeTmp
Run Build Command:"nmake" "/NOLOGO" "cmTC_b3144\fast"
Generator: execution of make failed. Make command was: "nmake" "/NOLOGO"
"cmTC_b3144\fast"
It seems that the GNU compilers are identified but don't seem to work. Any help would be much appreciated. I am trying to avoid using Cygwin.. but almost ready to go that route in a sec here.
To fix the problem I was having, I had to do two things.
use the command cmake -G "MinGW Makefiles" . (capital -G on windows)
update my CMakeLists.txt file to use gcc for C compiler and g++ for C++ compiler
CMakeLists.txt
cmake_minimum_required(VERSION 3.8)
set(CMAKE_C_COMPILER "C:/MinGW/bin/gcc.exe")
set(CMAKE_CXX_COMPILER "C:/MinGW/bin/g++.exe")
project (Tutorial)
add_executable(Tutorial tutorial.cpp)