5

I want to develop shared library in KDevelop. But i don't see any template for library.

I guess i have to create project from c++ template and edit CMake files in both projects. Unfortunately i have got no experience with library development with CMake, also i want good integration with KDevelop - automatic build of library when i build/run project which uses that library.

kravemir
  • 10,636
  • 17
  • 64
  • 111

1 Answers1

4

To create a library use the add_library command:

add_library(<name> [STATIC | SHARED | MODULE]
          [EXCLUDE_FROM_ALL]
          source1 source2 ... sourceN)

For example:

add_library(mylib SHARED
    a.h
    a.cpp
    b.h
    b.cpp)

Would create a shared library from the four files listed.

If your program (created with add_executable) uses the library, when you specify the link with target_link_libraries, CMake will add the dependency, so that if you changed a.cpp the library mylib would be rebuilt and your application would be re-linked.

For example

add_executable(myprog
    main.cpp)

target_link_libraries(myprog
    mylib)

Edit:

When your library and project are in different folders, you can use add_subdirectory.

Create a CMakeList.txt in each directory, in the library folder use add_library in the application, use add_program and target_link_libraries.

In the parent folder use add_subdirectory, add the library folder first, then the program folder. This will make the library available to the application. Then run cmake against the parent CMakeList.

Silas Parker
  • 8,017
  • 1
  • 28
  • 43
  • But the library and application are different projects and are in different folders. So how can i get the ProjectApplication to know about the ProjectLibrary? – kravemir Nov 17 '11 at 19:40
  • @MiroK I've expanded my answer to include `add_subdirectory` use. – Silas Parker Nov 17 '11 at 20:59
  • AFAIK, it's not necessary to include headers in add_library(). Is it true? – arrowd Nov 18 '11 at 05:56
  • 3
    Whilst not necessary for it to compile, it is best to include them so that a change in the header will cause a recompilation. *Make* and other build tools normally only check the last modified time, and so the fact they are included in other files would not cause the changes to be picked up, and trigger a recompilation. – Silas Parker Nov 18 '11 at 08:38
  • @kravemir If this post answered your question, please mark it so. – arrowd Sep 20 '16 at 07:43