Compile and link NS3 program with external library

You can compile and link your NS3 program with an external library. For example, your program “mysrc.cc” is under

NS3-BASE-DIR/examples/myapp/mysrc.cc

and you want to link to, e.g., a static library named “libfoo.a” that is under ~/LIBDIR/lib directory
and library header file exists under ~/LIBDIR/include.
First include the library header file into your “mysrc.cc” as

#include<foo.h>

Now open the NS3-BASE-DIR/examples/myapp/wscript and add build information for your program as follows

def build(bld):
    obj = bld.create_ns3_program('prog_name', ['core', 'network', """... modules your program depends on"""])
    obj.source = 'mysrc.cc'
    obj.env.append_value("CXXFLAGS", "-I/Absolute-path-to-LIBDIR/include")
    obj.env.append_value("LINKFLAGS", ["-L/Absolute-path-to-LIBDIR/lib"])
    obj.env.append_value("LIB", ["foo"])

This method is tested on NS-3.15.

5 Comments

    1. Could you help me?
      I´m trying to link the openssl library to ns3 but I cant make it work
      Thank you very much!

      Reply

      1. To link with dynamic library (*.so), you have to modify the LINKFLAGS as follows

        obj.env.append_value(“LINKFLAGS”,
        [“-L/Absolute-path-to-LIBDIR/lib”,
        “-Wl, -rpath, /Absolute-path-to-LIBDIR/lib”])

        See this post http://wp.me/p2engI-35
        Hope it helps.

Leave a comment