I am afraid the question I have might be stupid, but as I am new to kdevelop and cmake it is quite hard for me to understand how they work. The project I tried to set up uses the libnet 1.1 library. My Question is how do I get cmake to compile and link this library so I can use it properly?
Here is what I already tried:
PROJECT(test)
include_directories("${PROJECT_SOURCE_DIR}/libnet")
add_subdirectory(libnet)
ADD_EXECUTABLE(test main.c)
target_link_libraries(test libnet)
Thank you for your Help!
It looks like libnet does not use CMake itself, so you're going to have to build it separately or make it part of your own project.
To build it separately, you have a couple of choices. You can build it (and install it if you want) and then use
find_library
to locate the actual libnet.a / libnet.lib file.CMake provides a decent way to automate this through use of
ExternalProject_Add
. This is a little trickier to use, but you can make it download, extract, build and install libnet all in one command. It looks like libnet has several different ways of being built though, depending on platform, so this may not be too straightforward.Another option would be to include the libnet sources in your own project and add it as a library via
add_library
. You'd need to create a list of the libnet sources, and also examine the libnet makefiles to check for any compiler flags / oddities that would need special handling in your own CMakeLists.txtThis is perhaps the best option since it gives you access to the full libnet source tree in your IDE, allows you to fine-tune the libnet build, and causes your own project to go out of date (need rebuilding) if the libnet build changes.
You can make use of
file(GLOB...)
to help with generating the list of libnet sources, but it's not recommended since the addition or removal of a file would not be automatically detected by CMake. You need to make sure that if you do this, you re-run cmake manually before trying to recompile. This isn't an issue if you're not planning on adding/deleting any libnet files.Edit: Use ExternalProject Module
OK, there is a third option which is maybe the best, but can be slightly complex to set up; use CMake's
ExternalProject
Module. This is designed to allow building of external dependencies - even ones which don't use CMake. This is a decent article on using it.Try replacing your CMakeLists.txt with this (only tested on Ubuntu with gcc). In short, it downloads libnet, configures it, builds it and installs it to your build tree (not to /usr/local). Your executable can then include and link to it.