-1

This is a baffling issue for me, will be glad if someone has an insight.

I am tasked with converting an existing small c++ project that currently uses makefiles to one that is based on CMAKE. I am through the task, created CMakeLists.txt in all relevant folders and such. I then created a build folder inside the source tree and tried a build after running cmake. Everything builds correctly as it should (first builds 3 libs from other people's code, those are linked to this project's code etc.) and the status "make" command printed does show this. Finally, the command completes 100% with the last line saying [100%] Built target XXXXX

However, that target executable is nowhere to be found! I have tried explicitly setting CMAKE_ARCHIVE_OUTPUT_DIRECTORY to a directory in source (this is how we ideally want), some other folder, not setting it at all, etc. but to no avail. I create this executable using add_executable command and if I instead create a library from the same sources in the same location, the library (.a) gets created fine but I just cant seem to find this executable if I create it that make claims it built.

If it helps, the same project, if built on Windows, creates the EXE fine and I can see/run it in the folder inside source if I so specify.

(I am running g++/cmake on MacOS Sierra on the latest Macbook Pro that has SIP not turned off.)

Here is my minimal CMakeLists. The external libnames are replaced with LLLL. They are external libs in LLLL.a format in the lib directory.

cmake_minimum_required(VERSION 3.0)
project(my_exec C CXX)
cmake_policy(SET CMP0010 NEW)

set(CMAKE_MACOSX_RPATH TRUE)

SET(ENABLE_WARNINGS TRUE)

set(CMAKE_BUILD_TYPE Release)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -c -std=c++11 -w")

#Includes
include_directories(SYSTEM ${CMAKE_SOURCE_DIR}/inc/)

#External libs
set (EXT_LIBS LLLL LLLL LLLL)

#Where to find the libs above?
link_directories(${CMAKE_SOURCE_DIR}/lib/}) 

#Get all sources and headers
file(GLOB SOURCES ${CMAKE_SOURCE_DIR}/src/*.cpp)

#Finally!
add_executable(my_exec ${SOURCES})
target_link_libraries(my_exec ${EXT_LIBS})

message("Target path should be ${CMAKE_BINARY_DIR}")

Following is the cmake and build outputs and also find output which shows no exec

XXXX-MacBook-Pro:build XXXX$ rm -rf *
XXXX-MacBook-Pro:build XXXX$ cmake ..
-- The C compiler identification is AppleClang 9.0.0.9000037
-- The CXX compiler identification is AppleClang 9.0.0.9000037
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/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: /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
Target path should be /Users/XXXX/XXXX/XXXX/XXXX/build
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/XXXX/XXXX/XXXX/XXXX/build
XXXX-MacBook-Pro:build XXXX$ make -j34
Scanning dependencies of target my_exec
[  9%] Building CXX object CMakeFiles/my_exec.dir/src/aaaa.cpp.o
[ 18%] Building CXX object CMakeFiles/my_exec.dir/src/aaaa.cpp.o
[ 27%] Building CXX object CMakeFiles/my_exec.dir/src/aaaa.cpp.o
[ 36%] Building CXX object CMakeFiles/my_exec.dir/src/aaaa.cpp.o
[ 45%] Building CXX object CMakeFiles/my_exec.dir/src/aaaa.cpp.o
[ 54%] Building CXX object CMakeFiles/my_exec.dir/src/aaaa.cpp.o
[ 72%] Building CXX object CMakeFiles/my_exec.dir/src/aaaa.cpp.o
[ 72%] Building CXX object CMakeFiles/my_exec.dir/src/aaaa.cpp.o
[ 81%] Building CXX object CMakeFiles/my_exec.dir/src/aaaa.cpp.o
[ 90%] Building CXX object CMakeFiles/my_exec.dir/src/aaaa.cpp.o
[100%] Linking CXX executable my_exec
[100%] Built target my_exec

XXXX-MacBook-Pro:build XXXX$ cd ..
XXXX-MacBook-Pro:YYYY XXXX$ find . -name my*
./build/CMakeFiles/my_exec.dir
itsleo5
  • 1
  • 2
  • Have you checked out `${CMAKE_BINARY_DIR}` already? Usually, the output is found in the same directory, you ran `cmake ` - except, you explicitely change it. – camelCase Sep 27 '17 at 19:44
  • Possible duplicate of [CMake output/build directory](https://stackoverflow.com/questions/18826789/cmake-output-build-directory) – Mads Marquart Sep 27 '17 at 19:44
  • Variable `CMAKE_ARCHIVE_OUTPUT_DIRECTORY` affects only on *static libraries*. For **executables** set `CMAKE_RUNTIME_OUTPUT_DIRECTORY`. – Tsyvarev Sep 27 '17 at 19:48
  • Thanks, the CMAKE_BINARY_DIRECTORY is not set (if I print it in a message, nothing is there) and I have also tried setting CMAKE_RUNTIME_OUTPUT_DIRECTORY but no effect. – itsleo5 Sep 27 '17 at 19:53
  • Have you tried `message(STATUS "Output: $")` (where `name_of_program` is the first thing in `add_executable`)? – Travis Gockel Sep 27 '17 at 19:58
  • tried it - it only printed the excutable name (not sure if I did this right) the CMAKE_BINARY_DIR seems to be the build dir – itsleo5 Sep 27 '17 at 20:08
  • The `$` will give you the name of the file relative to `CMAKE_CURRENT_BINARY_DIR`. So `${CMAKE_CURRENT_BINARY_DIR}/$` will be where the executable is built to. If you're so inclined, you can use `get_filename_component` (https://cmake.org/cmake/help/v3.9/command/get_filename_component.html) to give you the absolute path. But this is overkill -- the outputs are usually in whatever directory you ran `cmake` in. – Travis Gockel Sep 27 '17 at 20:22
  • Yes, that has been my experience too. Surprisingly here, although cmake/make say they did exactly that, the executable is nowhere to be found. I suspect MacOS may be "eating" it up but I am unsure. – itsleo5 Sep 27 '17 at 20:36
  • Please, add `CMakeLists.txt` code (as [mcve]) into the question. – Tsyvarev Sep 28 '17 at 07:59

1 Answers1

0

I think I have figured out the answer that took me VERY long time. The problem is the one little CMAKE_CXX_FLAG I had: "-c". It should not be there at all. CMake (for unknown reasons) passes that flag on to the "driver" c++/g++ even during the linking phase causing the underlying compiler to be invoked instead of a linker and then the compiler complains BUT ONLY AS WARNINGS that it is getting all this non-sense like *.o files and -l flags. Linking never happens as a result and so executable is never generated. And on top of this, all this drama is hidden and never shown to the user unless one does make VERBOSE=1 and actually reads the last command and then tries to use the last command by themselves with -Wall or -Werror to reveal these compiler warnings that it is getting all these linker flags.

I think this is a CMake bug but I am unsure.

itsleo5
  • 1
  • 2