Linking ns3 as a third party dependency

154 views
Skip to first unread message

Jared Barnak

unread,
Nov 26, 2023, 12:16:41 PM11/26/23
to ns-3-users
Typically, when consuming third party packages, you only need to: 1.) Find the package, 2.) link the library, and 3.) include the public headers

```
cmake_minimum_required(VERSION 3.26)
project(net CXX)

add_executable(net main.cpp)

find_package(ns3 REQUIRED PATHS /home/jared/Installs/ns3/cmake-cache)

target_link_libraries(net PRIVATE
    ns3
)

target_include_directories(net PRIVATE /home/jared/Installs/ns3/build/include)
```

`find_package` looks for `ns3Config.cmake`, and as it is not part of the path, I
hard coded the value.

And it'll find  `ns3Config` just fine, but it won't be able to find `ns3Targets.cmake` which is defined within the `ns3Config.cmake` i

It has it located at:

```
/home/jared/Projects/ns-3-dev/cmake-cache/ns3Targets.cmake
```

when it's actually located at:

```
build/CMakeFiles/Export/2a81891a6eb0eac8fbb86ed7caf9a458/ns3Targets.cmake`
```

I would ideally like to create a minimal project and use the following headers:


```
#include "ns3/applications-module.h"
#include <ns3/core-module.h>
#include <ns3/flow-monitor-module.h>
#include <ns3/internet-module.h>
#include <ns3/point-to-point-module.h>
```

TIA!

Gabriel Ferreira

unread,
Nov 26, 2023, 1:10:05 PM11/26/23
to ns-3-users
ns3Targets.cmake and ns3Config.cmake get created in the appropriate places when installed.
If you want to use it without installing it, just make it an ExternalProject and consume the libraries directly. 

Jared Barnak

unread,
Nov 26, 2023, 5:32:38 PM11/26/23
to ns-3-users
It seems I'm still having trouble even when using `ExternalProject`?

```
cmake_minimum_required(VERSION 3.25.2)
project("NetSim")

add_executable(netsim src/main.cpp)

include(ExternalProject)
ExternalProject_Add(ns3
  GIT_REPOSITORY    g...@gitlab.com:nsnam/ns-3-dev.git
  #
  # BUILD_COMMAND     ${CMAKE_SOURCE_DIR}/build/ns3-prefix/src/ns3/ns3 configure && cmake --build ${CMAKE_SOURCE_DIR}/build/ns3-prefix/src/ns3/ns3
)

set(LIB_PATH ${CMAKE_SOURCE_DIR}/build/ns3-prefix/src/ns3/build/lib)
find_library(NS3_CORE libns3-dev-core-default HINTS ${LIB_PATH})

target_link_libraries(netsim PRIVATE
   ${NS3_CORE}
)
```

Gabriel Ferreira

unread,
Nov 26, 2023, 6:16:26 PM11/26/23
to ns-3-users
No need to use find_library, it's already available with libcore, libnetwork, etc
You can also add ns3:: prefix. 

Jared Barnak

unread,
Nov 26, 2023, 7:33:18 PM11/26/23
to ns-3-users
Mind providing a minimal example?

I've tried both:


```
cmake_minimum_required(VERSION 3.25.2)
project("NetSim")

add_executable(netsim main.cpp)


include(ExternalProject)
ExternalProject_Add(ns3
  GIT_REPOSITORY    g...@gitlab.com:nsnam/ns-3-dev.git
)

target_link_libraries(netsim PRIVATE
  ns3::libcore
  ns3::libnetwork
)
```

And

``
cmake_minimum_required(VERSION 3.25.2)
project("NetSim")

add_executable(netsim main.cpp)


include(ExternalProject)
ExternalProject_Add(ns3
  GIT_REPOSITORY    g...@gitlab.com:nsnam/ns-3-dev.git
)

target_link_libraries(netsim PRIVATE
  libcore
  libnetwork
)
```


And building with:
```
cmake -B build
cmake --build build
```

And I either can't find the target `ns3::libcore` or  I get a linking error

```
`/usr/bin/ld: cannot find -llibcore: No such file or directory
/usr/bin/ld: cannot find -llibnetwork: No such file or directory
collect2: error: ld returned 1 exit status
```

Additionally, I'm struggling to get `find_package` to work.
Reply all
Reply to author
Forward
0 new messages