1

I made a Non-Qt project, Plain C++ Application in QT creator. My current folder structure is like this:

.
├── client
│   ├── client.cpp
│   └── client.h
├── CMakeLists.txt
├── CMakeLists.txt.user
├── main.cpp
└── server
    ├── server.cpp
    └── server.h

My CMakeLists.txt file looks like this:

cmake_minimum_required(VERSION 3.5)

project(eshraagh-project LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_executable(eshraagh-project main.cpp server/server.h server/server.cpp client/client.h client/client.cpp)

In my main file, I included server.h and client.h and made instances of both of them, and it didn't give any errors.

The problem arises when I try to use an external library (this library). I have no idea how to use it.

I tried to copy contents of its src folder (cpnet-export.h, cpnet-network.h and cpnet-network.c) into my project and add them to CMakeLists, just like I did with server and client files, but it doesn't work. I copied the server part of the repository and pasted it in my main, but it gives me errors such as undefined reference to 'cpnet_bind' and more (A little note: The repository uses the functions with net prefix, but QT recommender told me to write cpnet prefix instead. I don't know why this is happening).

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Roozbeh Sayadi
  • 318
  • 2
  • 13
  • @lubgr Sorry my last comment was just a stupid error. I fixed it and you can find my new CMakeLists [here](https://paste.ubuntu.com/p/8bXGQcV3jh/). Now it gives me a new error: ```cannot find -llibcpnet```. – Roozbeh Sayadi May 17 '21 at 12:17
  • @RoozbehSayadi: On Stack Overflow we don't add "SOLVED" to the title. Having an accepted answer colors the question appropriately. BTW, you could even write an answer by yourself: https://stackoverflow.com/help/self-answer. – Tsyvarev May 17 '21 at 12:31

1 Answers1

2

Linking a library of an executable in CMake is achieved by using the target_link_libraries function. In your case,

target_link_libraries(eshraagh-project PRIVATE cpnet)

Here, cpnet could be the name of a target that you CMake project knows (otherwise, it is turned into some kind of platform specific -lcpnet linker flag). In order to make CMake know about the cpnet library, you can

  • add the external library sources to your project (as a git submodule, or by tracking them yourself), for this example let's say they the sources are located at lib/cpnet
  • tell CMake to include the configuration instructions from that folder. This works if the external project uses CMake, too, and doesn't mess with global variables and settings (this seems to be the case here).

So in your toplevel CMakeLists.txt:

add_subdirectory(lib/cpnet)

Note that this must be done before the target_link_libraries call.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
lubgr
  • 37,368
  • 3
  • 66
  • 117
  • You can also use `add_subdirectory("lib/cpnet" EXCLUDE_FROM_ALL)` so the `install()` commands in the subdirectory aren't used by default (from [this answer](https://stackoverflow.com/a/41805580/1658105)). – Androvich Mar 07 '23 at 09:34