Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake with "standard" directory layout (Linux)

Tags:

cmake

Let's say I have a simple hello project with the pseudo-standard directory layout

helloworld/
  src/
    main.c
    say.c
    say-helper.c
  include/
    say.h
    say-helper.h
  build/

and after running

cd ~/helloworld/build
cmake ..
make

I would expect the following

helloworld/
  build/lib/
    libsay.a
    libsay.so
    libsay.so.1.0.0
  tmp/obj/
    main.o
    say.o
  build/bin/
    hello

and after make install I would expect

/usr/local/lib/
  libsay.a
  libsay.so
  libsay.so.1.0.0
/usr/local/bin/
  hello

What would the CMakeLists.txt look like for this setup?

I've been looking around for examples, but the only one I've found that shows how to add a library and an executable didn't work.

like image 735
coolaj86 Avatar asked Feb 11 '11 00:02

coolaj86


1 Answers

Basic commands to describe the project:

INCLUDE_DIRECTORIES(include)
ADD_LIBRARY(say src/say.c src/say-helper.c)

ADD_EXECUTABLE(hello src/main.c)
TARGET_LINK_LIBRARIES(hello say)

This is for placing the libs and the executable in the build directory, put that in your CMakeLists.txt:

SET (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin)
SET (CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
SET (CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)

For install you specify

 install(TARGETS say hello
            RUNTIME DESTINATION bin
            LIBRARY DESTINATION lib
            ARCHIVE DESTINATION lib)

in your CMakeLists.txt and set CMAKE_INSTALL_PREFIX to /usr/local in your configuration.

I'm not sure if you can build static and dynamic libraries simultaneously with the same name, though. And I don't know how to tell CMake to put the obj files in some specific location.

like image 156
manol Avatar answered Oct 03 '22 12:10

manol