S
S
sitev_ru2017-06-13 15:28:59
C++ / C#
sitev_ru, 2017-06-13 15:28:59

How to connect lib to your project?

I rarely use CMake. Everything seems to be simple. But then suddenly I got an error that I can not cope with:

CMake Error at CMakeLists.txt:6 (add_subdirectory):
add_subdirectory not given a binary directory but the given source
directory "C:/projects/cmake/foo/src" is not a subdirectory of
"C:/projects/cmake/app/ src". When specifying an out-of-tree source a
binary directory must be explicitly specified.

The foo library is in the same folder as the app. The error disappears if you put the library inside app/src. The directory structure is as follows:
|-- app/src
|-- foo/src
The app/src folder contains the
CMakeLists.txt files:
cmake_minimum_required(VERSION 2.8) 
project(hello_world)				
set(SOURCE_EXE main.cpp)
include_directories(../../foo/src)
add_executable(main ${SOURCE_EXE})
add_subdirectory(../../foo/src)
target_link_libraries(main foo)

main.cpp:
#include "foo.h"
int main(int argc, char** argv)
{
  hello_world();
  return 0;
}

The foo/src folder contains the
CMakeLists.cpp files:
cmake_minimum_required(VERSION 2.8)
project(foo)				
set(SOURCE_LIB foo.cpp)		
add_library(foo STATIC ${SOURCE_LIB})

foo.h: foo.cpp:
void hello_world();
#include <iostream>
void hello_world()
{
  std::cout << "Hello, World!" << std::endl;
}

How to get rid of the error?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
tsarevfs, 2017-06-13
@sitev_ru

https://stackoverflow.com/a/15570165/1762922
You must explicitly specify the binary directory as the second parameter add_subdirectory (relative to the project's binary directory) for the library if it is not in the main project folder.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question