I have two shared libraries: liba and libb, where libb depends on liba and an executable which uses libb. I faced a problem building the project using CMake for aarch64 architecture, while everything works fine with host toolchain. The project looks as following:
├── CMakeLists.txt
├── liba
│ └── main.cpp
├── libb
│ └── main.cpp
└── main
└── main.cpp
CMakeLists.txt content is the following:
cmake_minimum_required(VERSION 3.15)
project(my_proj CXX)
add_library(a SHARED liba/main.cpp)
set_property(TARGET a PROPERTY LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/a")
add_library(b SHARED libb/main.cpp)
set_property(TARGET b PROPERTY LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/b")
target_link_libraries(b PRIVATE a)
add_executable(main main/main.cpp)
target_link_libraries(main b)
Everything works completely fine with my host g++/ld compiler/linker. However, when I try to build the project using ARM compiler, I get linker error:
$ cmake .. -DCMAKE_CXX_COMPILER=aarch64-linux-gnu-g++
-- The CXX compiler identification is GNU 9.2.0
-- Check for working CXX compiler: /usr/bin/aarch64-linux-gnu-g++
-- Check for working CXX compiler: /usr/bin/aarch64-linux-gnu-g++ -- 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: /home/icblnk/tmp/rm/test/build
$ make
Scanning dependencies of target a
[ 16%] Building CXX object CMakeFiles/a.dir/liba/main.cpp.o
[ 33%] Linking CXX shared library a/liba.so
[ 33%] Built target a
Scanning dependencies of target b
[ 50%] Building CXX object CMakeFiles/b.dir/libb/main.cpp.o
[ 66%] Linking CXX shared library b/libb.so
[ 66%] Built target b
Scanning dependencies of target main
[ 83%] Building CXX object CMakeFiles/main.dir/main/main.cpp.o
[100%] Linking CXX executable main
/usr/lib/gcc/aarch64-linux-gnu/9.2.0/../../../../aarch64-linux-gnu/bin/ld: warning: liba.so, needed by b/libb.so, not found (try using -rpath or -rpath-link)
/usr/lib/gcc/aarch64-linux-gnu/9.2.0/../../../../aarch64-linux-gnu/bin/ld: b/libb.so: undefined reference to `function_liba_1()'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/main.dir/build.make:85: main] Error 1
make[1]: *** [CMakeFiles/Makefile2:78: CMakeFiles/main.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
The problem is that CMake uses rpath ELF entries to find PRIVATE libraries, which for some reason doesn't work in case of ARM's linker. I tried to look into possible linker options, but failed to find anything useful.
compiler/linker versions I used in the example:
$ ld --version
GNU ld (GNU Binutils) 2.32
$ aarch64-linux-gnu-ld --version
GNU ld (GNU Binutils) 2.32
$ g++ --version
g++ (GCC) 9.2.0
$ aarch64-linux-gnu-g++ --version
aarch64-linux-gnu-g++ (GCC) 9.2.0
Linking fails with cross-compilation as sysroot is implicitly specified so when ld tries to find dependencies using rpath, it also prepends it with sysroot. The issue was fixed in e0f735f8f53543773f01a4f692609d1b23fd3621 and the fix is included since CMake 3.16.0. So now CMake also adds -rpath-link for private dependencies.
Related
I Written a very simple program in protobuf without services at all but when i run the code it throws erros like Example::Note::~Note() all information are given below please tell me where i'm making mistake.
example.proto
syntax = "proto3";
package Example;
message Note {
int32 id = 1;
string title = 2;
string description = 3;
}
i use protoc --cpp_out=. example.proto command to generate auto generated files
main.cpp
#include "example.pb.h"
#include <iostream>
int main(int argc, char const *argv[])
{
Example::Note note;
note.set_id(1);
note.set_title("This is Title");
note.set_description("This is Description");
std::cout << "Note Information: " << note.DebugString() << std::endl;
return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.14)
project(EXAMPLE LANGUAGES CXX)
# Find Protobuf installation
# Looks for protobuf-config.cmake file installed by Protobuf's cmake installation.
set(protobuf_MODULE_COMPATIBLE TRUE)
find_package(Protobuf CONFIG REQUIRED)
message(STATUS "Using protobuf ${Protobuf_VERSION}")
set(_PROTOBUF_LIBPROTOBUF protobuf::libprotobuf)
set(_PROTOBUF_PROTOC $<TARGET_FILE:protobuf::protoc>)
# Find gRPC installation
# Looks for gRPCConfig.cmake file installed by gRPC's cmake installation.
find_package(gRPC CONFIG REQUIRED)
message(STATUS "Using gRPC ${gRPC_VERSION}")
set(_GRPC_GRPCPP gRPC::grpc++)
set(_REFLECTION gRPC::grpc++_reflection)
set(_GRPC_CPP_PLUGIN_EXECUTABLE $<TARGET_FILE:gRPC::grpc_cpp_plugin>)
add_executable(Example
main.cpp
)
target_link_libraries(Example
Threads::Threads
${_REFLECTION}
${_GRPC_GRPCPP}
${_PROTOBUF_LIBPROTOBUF}
)
Error
.
.
.
-- Build files have been written to: /home/react-vision/dev/workingWithProtobuff/simpleExample/build
[ 50%] Building CXX object CMakeFiles/Example.dir/main.cpp.o
[100%] Linking CXX executable Example
/usr/bin/ld: CMakeFiles/Example.dir/main.cpp.o: in function `main':
main.cpp:(.text+0xf4): undefined reference to `Example::Note::~Note()'
/usr/bin/ld: main.cpp:(.text+0x12f): undefined reference to `Example::Note::~Note()'
/usr/bin/ld: CMakeFiles/Example.dir/main.cpp.o: in function `Example::Note::Note()':
main.cpp:(.text._ZN7Example4NoteC2Ev[_ZN7Example4NoteC5Ev]+0x22): undefined reference to `Example::Note::Note(google::protobuf::Arena*, bool)'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/Example.dir/build.make:153: Example] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/Example.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
The following CMakeLists.txt is enough for your example.proto and main.cpp file. It does not use gRPC because that wasn't relevant to testing.
cmake_minimum_required(VERSION 3.23)
project(example)
find_package(Protobuf REQUIRED)
protobuf_generate_cpp(example_srcs example_hdrs example.proto)
add_executable(main main.cpp ${example_srcs} ${example_hdrs})
target_include_directories(main PRIVATE "${CMAKE_CURRENT_BINARY_DIR}")
target_link_libraries(main PRIVATE protobuf::libprotobuf)
Below is a line-by-line explanation of this build and why it works.
Let's go line by line:
cmake_minimum_required(VERSION 3.23)
This must always be the first line of your top-level CMakeLists.txt file. It tells CMake which policies to enable. This is to ensure backwards compatibility, but not forwards compatibility, i.e. CMake will not emulate older versions, but will simply toggle back to old behaviors in certain cases. In particular, it will not stop you from using features that are too new for the stated minimum version, so you must always test your build with the version listed here. I wrote this answer using 3.23, but it will probably work with older versions.
project(example)
With very few exceptions, this will be the second line for your CMakeLists.txt. You can pick any project name you want. I chose example here because, well, this is an example. This command is what kicks off CMake's compiler detection routines. It is invalid to do most things before that has happened.
find_package(Protobuf REQUIRED)
This project uses protobuf so we use find_package to access CMake's first-party protobuf support (see documentation here). Because the project cannot possibly work without protobuf, we add the REQUIRED argument.
protobuf_generate_cpp(example_srcs example_hdrs example.proto)
Now we call a function provided by the find_package(Protobuf ...) module. This wraps a call to the protobuf compiler and defines two variables, example_srcs and example_hdrs (the names are arbitrary), to hold the files generated from example.proto. This isn't well-documented, but the generated files are placed in ${CMAKE_CURRENT_BINARY_DIR}.
add_executable(main main.cpp ${example_srcs} ${example_hdrs})
Now we create an executable target from your source file (main.cpp) and the aforementioned generated source files.
target_include_directories(main PRIVATE "${CMAKE_CURRENT_BINARY_DIR}")
The source files for main need to see the generated sources, so we add ${CMAKE_CURRENT_BINARY_DIR} to the list of include directories.
target_link_libraries(main PRIVATE protobuf::libprotobuf)
Finally, the application needs to link to the protobuf library, so we link to the imported protobuf::libprotobuf target. This, too, was created by the find_package(Protobuf ...) command.
To adjust this build to use Protobuf's own CMake support and to skip CMake's module, you will need to make the following changes:
Add CONFIG to find_package.
Replace the use of protobuf_generate_cpp with protobuf_generate.
This is what the build would look like:
cmake_minimum_required(VERSION 3.23)
project(example)
find_package(Protobuf CONFIG REQUIRED)
add_executable(main main.cpp)
protobuf_generate(TARGET main PROTOS example.proto)
target_include_directories(main PRIVATE "${CMAKE_CURRENT_BINARY_DIR}")
target_link_libraries(main PRIVATE protobuf::libprotobuf)
The CONFIG argument tells CMake to ignore any find modules that match (including its own first-party modules). Then the new command protobuf_generate replaces protobuf_generate_cpp. Now instead of populating source variables, you give it the target for which you're generating sources. The PROTOS argument is the list of protobuf files to compile.
Because it takes the target as an argument, it must follow add_exectuable, rather than precede it.
Here's how I built protobuf from sources to test this:
$ git clone --recursive git#github.com:protocolbuffers/protobuf.git
$ cmake -G Ninja -S protobuf -B _build/protobuf -DCMAKE_BUILD_TYPE=Release -Dprotobuf_BUILD_TESTS=NO -DCMAKE_INSTALL_PREFIX=$PWD/_local
$ cmake --build _build/protobuf/ --target install
Now I can compile the program in this example:
alex#alex-ubuntu:~/test$ cmake -G Ninja -S . -B _build/project -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH=$PWD/_local
-- The C compiler identification is GNU 9.4.0
-- The CXX compiler identification is GNU 9.4.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - 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: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found ZLIB: /usr/lib/x86_64-linux-gnu/libz.so (found version "1.2.11")
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE
-- Configuring done
-- Generating done
-- Build files have been written to: /home/alex/test/_build/project
alex#alex-ubuntu:~/test$ cmake --build _build/project/ --verbose
[1/4] cd /home/alex/test/_build/project && /home/alex/test/_local/bin/protoc-3.21.0.2 --cpp_out :/home/alex/test/_build/project -I /home/alex/test /home/alex/test/example.proto
[2/4] /usr/bin/c++ -I/home/alex/test/_build/project -isystem /home/alex/test/_local/include -O3 -DNDEBUG -MD -MT CMakeFiles/main.dir/main.cpp.o -MF CMakeFiles/main.dir/main.cpp.o.d -o CMakeFiles/main.dir/main.cpp.o -c /home/alex/test/main.cpp
[3/4] /usr/bin/c++ -I/home/alex/test/_build/project -isystem /home/alex/test/_local/include -O3 -DNDEBUG -MD -MT CMakeFiles/main.dir/example.pb.cc.o -MF CMakeFiles/main.dir/example.pb.cc.o.d -o CMakeFiles/main.dir/example.pb.cc.o -c /home/alex/test/_build/project/example.pb.cc
[4/4] : && /usr/bin/c++ -O3 -DNDEBUG CMakeFiles/main.dir/main.cpp.o CMakeFiles/main.dir/example.pb.cc.o -o main ../../_local/lib/libprotobuf.a -lpthread /usr/lib/x86_64-linux-gnu/libz.so && :
alex#alex-ubuntu:~/test$ ./_build/project/main
Note Information: id: 1
title: "This is Title"
description: "This is Description"
I'm trying to compile a simple program with SFML (basic helloworld but with a big green circle). I'm supposed to build the program on Linux for Linux and Windows. When I compile for Linux the program compile and run perfectly, however when I try to compile the Windows Executable, I get the following error:
/usr/bin/x86_64-w64-mingw32-ld: cannot find -lsfml-graphics
/usr/bin/x86_64-w64-mingw32-ld: cannot find -lsfml-window
/usr/bin/x86_64-w64-mingw32-ld: cannot find -lsfml-system
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/BrickShooter.dir/build.make:215: BrickShooter.exe] Error 1
make[1]: *** [CMakeFiles/Makefile2:76: CMakeFiles/BrickShooter.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
I tried to change the setting in my Cmake without success (add_library, include_directories) but so far nothing worked.
Here is my CMakeLists.txt:
cmake_minimum_required(VERSION 3.16)
project(BrickShooter)
set(CMAKE_CXX_STANDARD 14)
add_executable(BrickShooter main.cpp GameObjects/Block.cpp GameObjects/Block.h GameObjects/Player.cpp GameObjects/Player.h GameObjects/Shoot.cpp GameObjects/Shoot.h SysObjects/Game.cpp SysObjects/Game.h SysObjects/Store.cpp SysObjects/Store.h SysObjects/Menu.cpp SysObjects/Menu.h SysObjects/Backup.cpp SysObjects/Backup.h SysObjects/SysGame.cpp SysObjects/SysGame.h)
target_link_libraries (BrickShooter sfml-graphics sfml-window sfml-system)
And my toolchain for Windows:
# Sample toolchain file for building for Windows from an Ubuntu Linux system.
#
# Typical usage:
# *) install cross compiler: `sudo apt-get install mingw-w64`
# *) cd build
# *) cmake -DCMAKE_TOOLCHAIN_FILE=~/windows.cmake ..
set(CMAKE_SYSTEM_NAME Windows)
set(TOOLCHAIN_PREFIX x86_64-w64-mingw32)
# cross compilers to use for C, C++ and Fortran
set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}-gcc)
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}-g++)
set(CMAKE_Fortran_COMPILER ${TOOLCHAIN_PREFIX}-gfortran)
set(CMAKE_RC_COMPILER ${TOOLCHAIN_PREFIX}-windres)
# target environment on the build host system
set(CMAKE_FIND_ROOT_PATH /usr/${TOOLCHAIN_PREFIX})
# modify default behavior of FIND_XXX() commands
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(SFML_INCLUDE_DIR "gitlab_ci/SFML-2.5.1-WIN/include")
include_directories(gitlab_ci/SFML-2.5.1-WIN/include)
set(SFML_LIBRARY_DIR "gitlab_ci/SFML-2.5.1-WIN/lib")
set(SFML_DIR "gitlab_ci/SFML-2.5.1-WIN/lib/cmake/SFML")
I'm using the following command to manually build the program:
cmake gitlab_ci/SFML-2.5.1-WIN/ -DCMAKE_TOOLCHAIN_FILE=gitlab_ci/windows.cmake CMakeLists.txt && make
Where gitlab_ci/SFML-2.5.1-WIN is where I stored the includes, libs, etc for Windows. I'm on Ubuntu 20.04 LTS and I have installed everything I needed with the following commands:
apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y build-essential libsfml-dev cmake doxygen mingw-w64 curl unzip
curl -O https://www.sfml-dev.org/files/SFML-2.5.1-windows-gcc-7.3.0-mingw-64-bit.zip
unzip SFML-2.5.1-windows-gcc-7.3.0-mingw-64-bit.zip
mv SFML-2.5.1 gitlab_ci/SFML-2.5.1-WIN
rm SFML-2.5.1-windows-gcc-7.3.0-mingw-64-bit.zip
Thanks to #tsyvarev here is the solution that worked for me:
I added this line to my toolchain for Windows
link_directories(gitlab_ci/SFML-2.5.1-WIN/lib)
The final result is:
# Sample toolchain file for building for Windows from an Ubuntu Linux system.
#
# Typical usage:
# *) install cross compiler: `sudo apt-get install mingw-w64`
# *) cd build
# *) cmake -DCMAKE_TOOLCHAIN_FILE=~/windows.cmake ..
set(CMAKE_SYSTEM_NAME Windows)
set(TOOLCHAIN_PREFIX x86_64-w64-mingw32)
# cross compilers to use for C, C++ and Fortran
set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}-gcc)
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}-g++)
set(CMAKE_Fortran_COMPILER ${TOOLCHAIN_PREFIX}-gfortran)
set(CMAKE_RC_COMPILER ${TOOLCHAIN_PREFIX}-windres)
# target environment on the build host system
set(CMAKE_FIND_ROOT_PATH /usr/${TOOLCHAIN_PREFIX})
# modify default behavior of FIND_XXX() commands
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(SFML_INCLUDE_DIR "gitlab_ci/SFML-2.5.1-WIN/include")
include_directories(gitlab_ci/SFML-2.5.1-WIN/include)
set(SFML_LIBRARY_DIR "gitlab_ci/SFML-2.5.1-WIN/lib")
link_directories(gitlab_ci/SFML-2.5.1-WIN/lib)
set(SFML_DIR "gitlab_ci/SFML-2.5.1-WIN/lib/cmake/SFML")
and this give the following output:
cmake gitlab_ci/SFML-2.5.1-WIN/ -DCMAKE_TOOLCHAIN_FILE=gitlab_ci/windows.cmake CMakeLists.txt && make
-- The C compiler identification is GNU 9.3.0
-- The CXX compiler identification is GNU 9.3.0
-- Check for working C compiler: /usr/bin/x86_64-w64-mingw32-gcc
-- Check for working C compiler: /usr/bin/x86_64-w64-mingw32-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: /usr/bin/x86_64-w64-mingw32-g++
-- Check for working CXX compiler: /usr/bin/x86_64-w64-mingw32-g++ -- 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: /mnt/shared/school/Ynov/Home_Work/uf_dev_log_13/repo_gitlab
Scanning dependencies of target BrickShooter
[ 10%] Building CXX object CMakeFiles/BrickShooter.dir/main.cpp.obj
[ 20%] Building CXX object CMakeFiles/BrickShooter.dir/GameObjects/Block.cpp.obj
[ 30%] Building CXX object CMakeFiles/BrickShooter.dir/GameObjects/Player.cpp.obj
[ 40%] Building CXX object CMakeFiles/BrickShooter.dir/GameObjects/Shoot.cpp.obj
[ 50%] Building CXX object CMakeFiles/BrickShooter.dir/SysObjects/Game.cpp.obj
[ 60%] Building CXX object CMakeFiles/BrickShooter.dir/SysObjects/Store.cpp.obj
[ 70%] Building CXX object CMakeFiles/BrickShooter.dir/SysObjects/Menu.cpp.obj
[ 80%] Building CXX object CMakeFiles/BrickShooter.dir/SysObjects/Backup.cpp.obj
[ 90%] Building CXX object CMakeFiles/BrickShooter.dir/SysObjects/SysGame.cpp.obj
[100%] Linking CXX executable BrickShooter.exe
[100%] Built target BrickShooter
Here is the link to the CMake docs about link_directories()
I've been writing a compiler using LLVM as the backend. The CMake files I've written so far have worked on Linux, but I haven't had any luck on Windows. The project is split into a library and "driver" executable with their own CMakeLists.txt in separate subdirectories.
The top level CMakeLists.txt looks like this:
cmake_minimum_required (VERSION 3.7.0)
project (compiler)
set (CMAKE_CXX_STANDARD 14)
set (CMAKE_CXX_STANDARD_REQUIRED ON)
set (CMAKE_CXX_EXTENSIONS OFF)
find_package (LLVM REQUIRED CONFIG)
message (STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
message (STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
include_directories (${LLVM_INCLUDE_DIRS})
add_definitions(${LLVM_DEFINITIONS})
add_subdirectory (Compiler_Lib)
add_subdirectory (Compiler_exe)
The CMakeLists.txt for the library:
cmake_minimum_required (VERSION 3.7.0)
add_library (compiler_lib
AST.cpp
AST.h
parser.cpp
parser.h
scanner.cpp
scanner.h
token.cpp
token.h
visualizer.cpp
visualizer.h
codegen.cpp
codegen.h)
target_include_directories (compiler_lib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(compiler_lib LLVM)
And the CMakeLists.txt for the executable (which is where linking to the libraries fails):
cmake_minimum_required (VERSION 3.7.0)
project (compiler_exe)
add_executable (compiler_exe Compiler_exe.cpp getopt.h getopt.cpp)
target_link_libraries (compiler_exe LINK_PUBLIC LLVM compiler_lib)
I run the command "c:\Program Files\CMake\bin\cmake.exe" .. -G"MinGW Makefiles" -DCMAKE_PREFIX_PATH=C:\Users\James\llvm+clang-7.0.0-win64-msvc-release where C:\Users\James\llvm+clang-7.0.0-win64-msvc-release is the path to prebuilt LLVM libraries. However, running mingw32-make afterwards fails with the output
Scanning dependencies of target compiler_lib
[ 10%] Building CXX object Compiler_Lib/CMakeFiles/compiler_lib.dir/AST.cpp.obj
[ 20%] Building CXX object Compiler_Lib/CMakeFiles/compiler_lib.dir/parser.cpp.obj
[ 30%] Building CXX object Compiler_Lib/CMakeFiles/compiler_lib.dir/scanner.cpp.obj
[ 40%] Building CXX object Compiler_Lib/CMakeFiles/compiler_lib.dir/token.cpp.obj
[ 50%] Building CXX object Compiler_Lib/CMakeFiles/compiler_lib.dir/visualizer.cpp.obj
[ 60%] Building CXX object Compiler_Lib/CMakeFiles/compiler_lib.dir/codegen.cpp.obj
[ 70%] Linking CXX static library libcompiler_lib.a
[ 70%] Built target compiler_lib
Scanning dependencies of target compiler_exe
[ 80%] Building CXX object Compiler_exe/CMakeFiles/compiler_exe.dir/Compiler_exe.cpp.obj
[ 90%] Building CXX object Compiler_exe/CMakeFiles/compiler_exe.dir/getopt.cpp.obj
[100%] Linking CXX executable compiler_exe.exe
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: cannot find -lLLVM
collect2.exe: error: ld returned 1 exit status
Compiler_exe\CMakeFiles\compiler_exe.dir\build.make:102: recipe for target 'Compiler_exe/compiler_exe.exe' failed
mingw32-make[2]: *** [Compiler_exe/compiler_exe.exe] Error 1
CMakeFiles\Makefile2:176: recipe for target 'Compiler_exe/CMakeFiles/compiler_exe.dir/all' failed
mingw32-make[1]: *** [Compiler_exe/CMakeFiles/compiler_exe.dir/all] Error 2
Makefile:82: recipe for target 'all' failed
mingw32-make: *** [all] Error 2
This is the first time I've used CMake so I could have missed something obvious, but as I say it seems to work on Linux.
For the linking to succeed two things need to be true:
1) the file libLLVM.a needs to exist
2) that file has to be in a directory in the library search path
There should be a way to get cmake to tell you the list of places it searches for libraries, and you need to find a way to get wherever libLLVM.a exists into that list of dirs.
Apologies for the partial answer, but that's the troubleshooting path...
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
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.
I am trying to compile the conv-net library in my mac osx yosemite with xcode. I even set the flags to libstdc++ still it is not linking properly.
i still get undefined symbols for architecture x86_64.
any help much appreciated.
sh-3.2# ./compile.sh
Building conv-net library
-- The C compiler identification is AppleClang 6.0.0.6000056
-- The CXX compiler identification is AppleClang 6.0.0.6000056
-- 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
-- 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
-- Configuring done
WARNING: Target "testimg" requests linking to directory "/usr/local/Cellar/opencv/2.4.9/lib". Targets may link only to libraries. CMake is dropping the item.
WARNING: Target "testimg" requests linking to directory "/usr/local/Cellar/opencv/2.4.9/lib". Targets may link only to libraries. CMake is dropping the item.
WARNING: Target "testmnist" requests linking to directory "/usr/local/Cellar/opencv/2.4.9/lib". Targets may link only to libraries. CMake is dropping the item.
WARNING: Target "testmnist" requests linking to directory "/usr/local/Cellar/opencv/2.4.9/lib". Targets may link only to libraries. CMake is dropping the item.
-- Generating done
-- Build files have been written to: /var/tmp/conv-net-0.1-prealpha_buildroot
Scanning dependencies of target cvconvnet
[ 11%] Building CXX object CMakeFiles/cvconvnet.dir/src/cvsubsamplingplane.cpp.o
[ 22%] Building CXX object CMakeFiles/cvconvnet.dir/src/cvconvolutionplane.cpp.o
[ 33%] Building CXX object CMakeFiles/cvconvnet.dir/src/cvgenericplane.cpp.o
[ 44%] Building CXX object CMakeFiles/cvconvnet.dir/src/cvsourceplane.cpp.o
[ 55%] Building CXX object CMakeFiles/cvconvnet.dir/src/cvrbfplane.cpp.o
[ 66%] Building CXX object CMakeFiles/cvconvnet.dir/src/cvmaxplane.cpp.o
[ 77%] Building CXX object CMakeFiles/cvconvnet.dir/src/cvconvnetparser.cpp.o
[ 88%] Building CXX object CMakeFiles/cvconvnet.dir/src/cvfastsigmoid.cpp.o
[100%] Building CXX object CMakeFiles/cvconvnet.dir/src/cvconvnet.cpp.o
/Users/prabhubalakrishnan/Desktop/conv-net/src/cvconvnet.cpp:169:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
1 warning generated.
Linking CXX static library libcvconvnet.a
[100%] Built target cvconvnet
Scanning dependencies of target testimg
Linking CXX executable testimg
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [testimg] Error 1
make[1]: *** [CMakeFiles/testimg.dir/all] Error 2
make: *** [all] Error 2
cp: testimg: No such file or directory
cp: testmnist: No such file or directory
ls: illegal option -- I
usage: ls [-ABCFGHLOPRSTUWabcdefghiklmnopqrstuwx1] [file ...]
This is my make file
cmake_minimum_required(VERSION 3.0)
PROJECT (CvConvolutionalNet)
set (CMAKE_CXX_FLAGS " -stdlib=libstdc++")
# IF() ENDIF() statements
SET(CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS true)
# Specify build-type as Debug if not specified already
IF (NOT CMAKE_BUILD_TYPE)
SET(CMAKE_BUILD_TYPE Debug)
ENDIF ()
# Produce verbose makefiles
# SET(CMAKE_VERBOSE_MAKEFILE ON)
# Sources for library
SET(CVCONVNET_SRCS
src/cvsubsamplingplane.cpp
src/cvconvolutionplane.cpp
src/cvgenericplane.cpp
src/cvsourceplane.cpp
src/cvrbfplane.cpp
src/cvmaxplane.cpp
src/cvconvnetparser.cpp
src/cvfastsigmoid.cpp
src/cvconvnet.cpp
)
# Sources for test files
SET(TESTIMG_SRCS tst/)
SET(TESTMNIST_SRCS tst/)
set (CV_H /usr/local/Cellar/opencv/2.4.9/include/opencv)
set (HIGHGUI_H src/include )
set (LIBCV /usr/local/Cellar/opencv/2.4.9/lib )
set (LIBHIGHGUI /usr/local/Cellar/opencv/2.4.9/lib )
# Here are common paths (in addition to default paths)
SET (INCLUDE_SEARCH_PATH
/usr/local/include /usr/include /usr/include/opencv/
/usr/include/opencv/ c:/program\ files/opencv/include
)
SET (LIBRARY_SEARCH_PATH
/usr/local/lib /usr/lib c:/program\ files/opencv/lib c:/windows/system32
)
# Find OpenCV and Expat
FIND_PATH(CV_H NAMES cv.h PATHS ${INCLUDE_SEARCH_PATH} )
FIND_PATH(HIGHGUI_H NAMES highgui.h PATHS ${INCLUDE_SEARCH_PATH} )
FIND_PATH(EXPAT_H NAMES expat.h PATHS ${INCLUDE_SEARCH_PATH} )
FIND_LIBRARY(LIBCV NAMES cv PATHS ${LIBRARY_SEARCH_PATH} )
FIND_LIBRARY(LIBHIGHGUI NAMES highgui PATHS ${LIBRARY_SEARCH_PATH} )
FIND_LIBRARY(LIBEXPAT NAMES expat PATHS ${LIBRARY_SEARCH_PATH} )
INCLUDE_DIRECTORIES(include/ ${CV_H} ${HIGHGUI_H} ${EXPAT_H})
# Here is out library
ADD_LIBRARY(cvconvnet STATIC ${CVCONVNET_SRCS})
# Here are our test programs
ADD_EXECUTABLE(testimg ${TESTIMG_SRCS})
ADD_EXECUTABLE(testmnist ${TESTMNIST_SRCS})
# Compiler options are different for Release and Debug
IF (CMAKE_BUILD_TYPE MATCHES Release)
# Highly optimized + cancel all assert()s
ADD_DEFINITIONS(-O3 -DNDEBUG)
ELSE ()
# Include debug info, profiling info, some text output
ADD_DEFINITIONS(-O -pg -g -DDEBUG)
# Set profiling for linker too
SET_TARGET_PROPERTIES(testmnist PROPERTIES LINK_FLAGS "-pg")
ENDIF ()
# We should link our test programs to libraries
TARGET_LINK_LIBRARIES(testimg cvconvnet ${LIBCV} ${LIBHIGHGUI} ${LIBEXPAT})
TARGET_LINK_LIBRARIES(testmnist cvconvnet ${LIBCV} ${LIBHIGHGUI} ${LIBEXPAT})
I fixed it by adding -c option in the compiler flags and it worked !!!!
The add_executable command takes a target name and a list of source files to be compiled. When you are creating a target for testimg you are passing it directory.
Use file(GLOB ...) command to gather all source files from directory into a list variable and pass it to add_executable call.