3

I've got a custom toolchain. I set CMAKE_AR variable with path to a proper ar program. However while I am building a static library, I've got the error:

cmd.exe /C "cd . && "C:\Program Files (x86)\CMake\bin\cmake.exe" -E remove Library\libLibrary.a && "" qc Library\libLibrary.a  Library/CMakeFiles/Library.dir/src/Library.cpp.obj && cd ."
'""' is not recognized as an internal or external command,

If I noticed correctly, CMake is using CMAKE_CXX_ARCHIVE_CREATE variable to create library. Its default value is:

set(CMAKE_CXX_ARCHIVE_CREATE "<CMAKE_AR> qc <TARGET> <LINK_FLAGS> <OBJECTS>")

(https://github.com/Kitware/CMake/blob/v3.10.3/Modules/CMakeCXXInformation.cmake) The problem is that part is not replaced by CMAKE_AR variable. It is weird, because and seem to be correct. I know that I can change in CMAKE_CXX_ARCHIVE_CREATE by replacing it by path to ar program, but I would have to also change other variables.

CMake: 3.10.2 OS: Windows 10

It seems to be a bug in CMake.

iblis
  • 181
  • 2
  • 9
  • You don't show **how** do you set *CMAKE_AR* variable in your toolchain. See also this [my answer](https://stackoverflow.com/a/43777707/3440745). – Tsyvarev Apr 11 '18 at 08:41
  • same here when passing `-DCMAKE_AR=${AR}` on the command line with cmake 3.13.2 - do you mind opening a bug in https://gitlab.kitware.com/cmake/cmake/issues ? I am not using a toolchain but a custom `CMAKE_OSX_SYSROOT` – Ax3l Jan 08 '19 at 23:07
  • I am having the exact same problem with cmake 3.10.2. I set CMAKE_AR in a toolchain file. I also set my c and c++ compilers in the same toolchain file. The compilers are found with their correct paths. The ar command is left as "" (blank) in the resulting Makefile, causing try_compile() to fail. SET(CMAKE_AR "x86_64-w64-mingw32-ar") SET(CMAKE_C_COMPILER "x86_64-w64-mingw32-gcc") – Ken Apr 19 '19 at 13:37

2 Answers2

2

I found another reference to someone having a problem with CMAKE_AR here. They mentioned that it only worked if the value was cached. I tried adding cache settings in my toolchain file and it worked. Notice that only the value for CMAKE_AR needs to be cached, and not the values for compilers. Seems like a bug in CMake.

SET(CMAKE_C_COMPILER       "x86_64-w64-mingw32-gcc") 
SET(CMAKE_CXX_COMPILER     "x86_64-w64-mingw32-g++") 
SET(CMAKE_FORTRAN_COMPILER "x86_64-w64-mingw32-gfortran") 
SET(CMAKE_AR               "x86_64-w64-mingw32-ar" CACHE FILEPATH "" FORCE) 
Ken
  • 309
  • 2
  • 11
  • This worked for me on Linux to armhf-poky. Thanks very much for this @Ken, I've been stuck for *hours*. – Kingsley Jan 16 '23 at 02:50
0

It was my fault. I set CMAKE_TOOLCHAIN while calling cmake:

cmake -G "Ninja" -DCMAKE_TOOLCHAIN_FILE=../app/toolchain.cmake ../app

However I didn't include this toolchain.cmake in CMakeLists.txt. I thought that setting CMAKE_TOOLCHAIN is enough. It is quite weird that changes of some variables ware visible, but not all of them.

iblis
  • 181
  • 2
  • 9