0

I have some simple implementation of LinkedList in file list. To crate library out of it I'm using cmake, my CMakelists.txt looks like that:

cmake_minimum_required(VERSION 2.6) 
project(LinkedList)
set(CMAKE_CXX_FLAGS "-o -Wall")

include_directories(${LinkedList_SOURCE_DIR})
link_directories(${LinkedList_BINARY_DIR})

add_executable(list list.c) 
add_library(listStatic STATIC list.c)
add_library(listShared SHARED list.c)

Everything works fine. But now it comes to use my library in other programs.

As far as I know listShared.h is required to use this library in my program and listStatic.h for static library.

Is there a way to automatically generate header file in CMake, so I can #include those libraries in other programs?

Or I just don't understand how it all works?

usr1234567
  • 21,601
  • 16
  • 108
  • 128
Purple
  • 711
  • 2
  • 10
  • 19
  • 1
    What would be the content of `listShared.h` and `listStatic.h`? Usually you don't need specific headers, but you have to provide a `list.h`. It cannot be generated automatically, as you have to specify which functions or symbols you want to make accessible through the header and which should be hidden in the c file. – usr1234567 Mar 15 '15 at 11:13
  • Are you saying that, because you've created a library `listStatic` you expect that users have to `#include` a similarly named header? If so, that's not the case. The headers you have to include depend on how the library is written, not on your CMake config. Clients only need to `#include "listStatic.h"` if the library's code is written to require that. – bames53 Mar 15 '15 at 11:16
  • @usr1234567 I wanted to listShared.h and listStatic.h list of all functions and structures i use i list.c. But you are right, I forgot that I need to specify which will be available. – Purple Mar 15 '15 at 11:16
  • @bames53 So, is there way to don't `#include "listStatic.h"`, but only link the library during compilation? – Purple Mar 15 '15 at 11:19
  • Yes, you can create the library's headers using whatever name you like; There's no need for the library's header file names to correspond to the cmake `add_library(library_name)` call. There's also no particular need for the header file content to differ between the shared library and the static library, so they can both use the same header. – bames53 Mar 15 '15 at 11:26
  • @Purple: Great. I summarized these comments in an answer. Please accept it, then the question gets marked as solved and other people don't try to help you anymore. – usr1234567 Mar 15 '15 at 12:55

1 Answers1

0

There is no need for specific headers for static and dynamic libraries. You have to provide only one header list.h which can be used for both purposes. It contains a list of all functions and structures declarations you use in list.c and which should be usable by other programs, too.

To create and use either type of libraries, you have to change the parameters for the compiler respectively the linker. In CMake this is done by passing add_library the key word STATIC or SHARED.

usr1234567
  • 21,601
  • 16
  • 108
  • 128