0

As you can see form here (How to Specify the Compiler and To Compile Different Main Functions) I am new to CMake and I still have problem. The previous question helped me to understand CMake, but I still have problems. First of all, even if I wrote the following lines in my CMakeLists.txt

set(CMAKE_CXX_COMPILER /opt/local/bin/g++)
set(CMAKE_CXX_FLAGS "-Wall -std=c++11")

the compiler used seems to be Clang and not g++. In addition when compiling (I get rid of all code errors) I obtain this message:

[ 33%] Built target Output
[ 66%] Built target Vector
Scanning dependencies of target Output_test
[100%] Building CXX object tests/CMakeFiles/Output_test.dir/output_test.cpp.o
Linking CXX executable Output_test
ld: library not found for -lIO
collect2: error: ld returned 1 exit status
make[2]: *** [tests/Output_test] Error 1
make[1]: *** [tests/CMakeFiles/Output_test.dir/all] Error 2
make: *** [all] Error 2

Do you know what I am doing wrong?

Edit: In the root folder

cmake_minimum_required (VERSION 2.8.11)
project (MC)

set(CMAKE_CXX_COMPILER /opt/local/bin/g++)
set(CMAKE_CXX_FLAGS "-Wall -std=c++11")

# Class definitions
add_subdirectory(IO)
add_subdirectory(math)

# Tests
add_subdirectory(tests)

In the IO folder:

add_library(Output Output.cpp)
Community
  • 1
  • 1
  • 1
    You might want to edit the question to include the part of the `CMakeLists.txt` file that checks for the `IO` library. – Some programmer dude Sep 29 '14 at 18:58
  • @JoachimPileborg Done, sorry. –  Sep 29 '14 at 19:28
  • Did you try removing all old files produced by CMake and starting over? – Svalorzen Sep 29 '14 at 21:07
  • @Svalorzen Yes, several times. –  Sep 29 '14 at 21:21
  • You should probably post your whole directory/file setup, and all the contents of all your CMake files. – Svalorzen Sep 29 '14 at 22:53
  • In the `tests/CMakeLists.txt` file, how do you tell the linker to link with the `IO` library? Do you add the path to the library (with a `-L` linker flag)? And please show a complete ***verbose*** build. – Some programmer dude Sep 30 '14 at 07:17
  • 4
    Please do not try to force CMake to use a specific compiler by setting `CMAKE_CXX_COMPILER` in the `CMakeLists.txt`. Unless you are doing cross-compilation, messing with this variable is a horrible idea. Instead, leave it to the user to select the correct compiler [when invoking `cmake` on the command line](http://www.cmake.org/Wiki/CMake_FAQ#How_do_I_use_a_different_compiler.3F). – ComicSansMS Sep 30 '14 at 07:32

1 Answers1

0

Looks like you named the library in the IO directory Output. So you want to use that name in your target_link_libraries command for the executable. Like @ComicSansMS said, don't use set to override the compiler. See http://www.cmake.org/Wiki/CMake_FAQ#How_do_I_use_a_different_compiler.3F

HughB
  • 363
  • 2
  • 12