Cmake build failed with CMAKE_AR-NOTFOUND (cr exe not found) - c++

cmake build failed with
CMAKE_AR-NOTFOUND cr libperfutils.a
When i checked build folder ar is not set.
x86_64-linux\gtest-native\1.7.0-r5\build\CMakeCache.txt
CMAKE_AR:FILEPATH=CMAKE_AR-NOTFOUND
//CXX compiler.
CMAKE_CXX_COMPILER:FILEPATH=/usr/bin/g++
How to set ar in cmake ?
Wheni see the cmake config below output . cmake automatically finding the g++
& why it not detecting ar
-- The CXX compiler identification is GNU 4.8.4
-- Check for working CXX compiler: g++
-- Check for working CXX compiler: g++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
When i check ar in machine manually its available.
/usr/bin/ar

CMake should have been able to detect /usr/bin/ar by itself, but you can manually override this, with the -D CMAKE_AR=/usr/bin/ar option.
Here's the list of useful variables:
https://gitlab.kitware.com/cmake/community/wikis/doc/cmake/Useful-Variables

cmake ar should set in cmake lib build ,
temperorly solved the issue by manally setting in my local Cmake file
set( CMAKE_AR "${AR}" CACHE FILEPATH "Archiver" )

Related

cmake generated header causes double build

A very simple scenario - I need to generate header, include it and if generated header is updated while cmake build, all dependent cpp units also must be rebuilt.
A simplified example:
add_custom_command(
OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/test.h
COMMAND ...
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/test.in"
add_custom_target(test_target DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/test.h)
add_executable(test_exe
${CMAKE_CURRENT_SOURCE_DIR}/main.cpp
)
add_dependencies(test_exe test_target)
main.cpp is just:
#include "test.h"
int main()
{
return 0;
}
When I do a full rebuild, everything goes fine. But when I change test.in, run just cmake.exe --build . --target all only test.h is regenerated, but main.cpp is not recompiled. But when I run cmake.exe --build . --target all again(second time), main.cpp is recompiled and test_exe is relinked.
What do I wrong?
P.S. If I explicitly use OBJECT_DEPENDS, there is no issue, rebuild works fine, but the docs say it's not required anymore - https://cmake.org/cmake/help/v3.20/prop_sf/OBJECT_DEPENDS.html
Update:
I use Windows 10, CMake 3.19.2 and Ninja 1.10.2
Solution:
Use build directory inside your project dir, then there is no the problem. But if the build directory is outside of our project directory, then there is the problem.
Posting as an answer to be able to show full example
CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
project(example)
add_custom_command(
OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/test.h
COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_CURRENT_SOURCE_DIR}/test.in" ${CMAKE_CURRENT_SOURCE_DIR}/test.h
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/test.in")
add_custom_target(test_target DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/test.h)
add_executable(test_exe
${CMAKE_CURRENT_SOURCE_DIR}/main.cpp
)
add_dependencies(test_exe test_target)
main.cpp
#include "stdio.h"
#include "test.h"
int main()
{
printf("The value is %d\n", FOO);
return 0;
}
test.in
#define FOO 4
This works on windows with CMake 3.19.2 and Ninja 1.10.2, one can change the FOO define in test.in after an initial build and see the resultant executable be rebuilt and see it's value changed.
The commands used to test
$ cmake -B build -G Ninja
-- The C compiler identification is Clang 11.0.0 with GNU-like command-line
-- The CXX compiler identification is Clang 11.0.0 with GNU-like command-line
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files/LLVM/bin/clang.exe - 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: C:/Program Files/LLVM/bin/clang++.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: <some_path>
$ cmake --build build
[3/3] Linking CXX executable test_exe.exe
$ ./build/test_exe.exe
The value is 4
$ echo "#define FOO 3" > test.in
$ cmake --build build
[3/3] Linking CXX executable test_exe.exe
$ ./build/test_exe.exe
The value is 3

Covert gcc arguments to CMake file

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}\""
)

cmake mingw-w64: strange error when trying to build

I have installed MinGW-w64 and built a simple Hello World Program. But the second time I run cmake --build . a strange error occurs.
When the Path variable is set to C:/MinGW/bin everything is fine and works. But when I set the Path to C:/msys64/mingw64/bin this happens:
PS C:\repos\hellovs\build> cmake ../ -G Ninja
-- The CXX compiler identification is GNU 10.1.0
-- Check for working CXX compiler: C:/msys64/mingw64/bin/c++.exe
-- Check for working CXX compiler: C:/msys64/mingw64/bin/c++.exe - 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: C:/repos/hellovs/build
PS C:\repos\hellovs\build> cmake --build ./
[2/2] Linking CXX executable HelloVS.exe
PS C:\repos\hellovs\build> cmake --build ./
ninja: error: FindFirstFileExA(c/:/msys64/mingw64/include/c++/10.1.0): ╤шэЄръёшўхёър  ю°шсър т шьхэш Їрщыр, шьхэш яряъш шыш ьхЄъх Єюьр.
HelloVS.exe compiles normally the first time the build command is run.
Apparanteley Ninja tryies to use an invalid path. Any idea about how to fix this?
Seems like invalid dependencies have been generated:
PS C:\repos\hellovs\build> ninja -t recompact
PS C:\repos\hellovs\build> ninja -t deps
CMakeFiles/HelloVS.dir/main.cpp.obj: #deps 136, deps mtime 6109023486207704 (VALID)
../main.cpp
C/:/msys64/mingw64/include/c++/10.1.0/iostream
C/:/msys64/mingw64/include/c++/10.1.0/x86_64-w64-mingw32/bits/c++config.h
C/:/msys64/mingw64/include/c++/10.1.0/x86_64-w64-mingw32/bits/os_defines.h
C/:/msys64/mingw64/include/c++/10.1.0/x86_64-w64-mingw32/bits/cpu_defines.h
C/:/msys64/mingw64/include/c++/10.1.0/pstl/pstl_config.h
C/:/msys64/mingw64/include/c++/10.1.0/ostream
...
It's a GCC bug.
MSYS2 maintainers decided to not wait for the upstream and patched it.
Run pacman -Syuu from MSYS2 shell to update your packages.

How to use lld with CMake on Windows?

I'm trying to compile SDL2 (https://www.libsdl.org/download-2.0.php) using CMake with clang + lld (http://releases.llvm.org/) + mingw (https://sourceforge.net/projects/mingw-w64/) headers on Windows 10. Despite my many efforts, I seem unable to get CMake to use the lld linker over the mingw ld linker.
I currently build sdl2 with a batch file:
#ECHO OFF
IF NOT EXIST build MKDIR build
PUSHD build
cmake.exe -DCMAKE_BUILD_TYPE=Debug -G "MinGW Makefiles"^
-DCMAKE_C_FLAGS="-target x86_64-windows-gnu"^
-DCMAKE_C_COMPILER_ID="Clang" -DCMAKE_C_COMPILER="clang.exe"^
-DCMAKE_CXX_FLAGS="-target x86_64-windows-gnu"^
-DCMAKE_CXX_COMPILER_ID="Clang++" -DCMAKE_CXX_COMPILER="clang++.exe"^
-DDIRECTX=OFF -DSDL_TEST=OFF -DSDL_SHARED=OFF -DSDL_STATIC=ON ..
cmake.exe --build . -- -j %NUMBER_OF_PROCESSORS%
POPD
I have tried to no avail:
setting -fuse-ld=lld.exe
setting LDFLAGS=lld.exe
setting -DCMAKE_LINKER=lld.exe
the solution from: CMake: use a custom linker
Any help would be greatly appreciated.
To replicate your environment, I followed these steps:
I installed LLVM+Clang 12 to C:\Program Files\LLVM from the GitHub releases page: https://github.com/llvm/llvm-project/releases/download/llvmorg-12.0.0/LLVM-12.0.0-win64.exe
I downloaded mingw-w64 via MSYS2. I have it installed to C:\msys64. See: https://www.msys2.org/
I then downloaded SDL2 2.0.14 from https://www.libsdl.org/release/SDL2-2.0.14.zip and unzipped it to D:\SDL2-2.0.14
I have CMake 3.20 and Ninja 1.10.2 installed system-wide and in the PATH.
Then, I created D:\clang-mingw.cmake with the following contents:
set(CMAKE_C_COMPILER "C:/Program Files/LLVM/bin/clang.exe")
set(CMAKE_CXX_COMPILER "C:/Program Files/LLVM/bin/clang++.exe")
set(CMAKE_C_COMPILER_TARGET x86_64-windows-gnu)
set(CMAKE_CXX_COMPILER_TARGET x86_64-windows-gnu)
set(CMAKE_SYSROOT "C:/msys64/mingw64")
set(CMAKE_EXE_LINKER_FLAGS_INIT "-fuse-ld=lld")
set(CMAKE_MODULE_LINKER_FLAGS_INIT "-fuse-ld=lld")
set(CMAKE_SHARED_LINKER_FLAGS_INIT "-fuse-ld=lld")
This is called a toolchain file. These are used to inform CMake about non-standard compiler and library setups like this one.
Setting the CMAKE_<LANG>_COMPILER variables (naturally) points CMake to the compilers you want to use. In this case, that's Clang.
Setting CMAKE_<LANG>_COMPILER_TARGET configures Clang to use the x86_64-windows-gnu target when compiling. This is important for CMake's compiler detection and sanity checking steps.
Setting CMAKE_SYSROOT informs Clang where to find all of the standard libraries and headers (ie. those from mingw-w64).
Finally, we ensure LLD is used by adding -fuse-ld=lld to the linker flags used when linking executables, loadable modules, and shared libraries (but not static libraries, since no linker is needed, just the archiver). The CMAKE_{EXE,MODULE,SHARED}_LINKER_FLAGS_INIT variables are meant to be set from the toolchain for this purpose.
Then from a normal command prompt (i.e. not a Visual Studio command prompt or MSYS2 bash), I ran:
D:\SDL2-2.0.14>cmake -G Ninja -S . -B build -DCMAKE_TOOLCHAIN_FILE=D:/clang-mingw.cmake -DCMAKE_BUILD_TYPE=Debug -DDIRECTX=OFF -DSDL_TEST=OFF -DSDL_SHARED=OFF -DSDL_STATIC=ON
-- The C compiler identification is Clang 12.0.0
-- The CXX compiler identification is Clang 12.0.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files/LLVM/bin/clang.exe - 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: C:/Program Files/LLVM/bin/clang++.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Could NOT find PkgConfig (missing: PKG_CONFIG_EXECUTABLE)
-- Performing Test HAVE_GCC_WALL
-- Performing Test HAVE_GCC_WALL - Success
[... output clipped ...]
The configure command I used does little more than replicate the SDL-specific settings you used in your question. It just selects Ninja as the build system generator (you could of course replace this with a different one if you so desired) and sets the toolchain file to the one above.
It then builds without errors.
D:\SDL2-2.0.14>cmake --build build
[... output clipped ...]
[163/164] C:\PROGRA~1\LLVM\bin\clang.exe --target=x86_64-windows-gnu --sysroot=C:/msys64/mingw64 -DUSING_GENERATED_CONFIG_H -Iinclude -I../include -idirafter "D:/SDL2-2.0.14/src/video/khronos" "-ID:/SDL2-2.0.14/src/hidapi/hidapi" -msse3 -msse2 -msse -m3dnow -mmmx -Wshadow -fvisibility=hidden -Wdeclaration-after-statement -Werror=declaration-after-statement -fno-strict-aliasing -Wall -g -MD -MT CMakeFiles/SDL2-static.dir/src/video/yuv2rgb/yuv_rgb.c.obj -MF CMakeFiles\SDL2-static.dir\src\video\yuv2rgb\yuv_rgb.c.obj.d -o CMakeFiles/SDL2-static.dir/src/video/yuv2rgb/yuv_rgb.c.obj -c ../src/video/yuv2rgb/yuv_rgb.c
[164/164] cmd.exe /C "cd . && "C:\Program Files\CMake\bin\cmake.exe" -E rm -f libSDL2d.a && C:\PROGRA~1\LLVM\bin\llvm-ar.exe qc libSDL2d.a #CMakeFiles\SDL2-static.rsp && cd ."

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.