Network Simulator - ns-3: Difference between revisions

From NixOS Wiki
imported>EyJhb
ns-3 is a discrete-event network simulator for Internet systems, targeted primarily for research and educational use. This shows how to do development using ns-3
 
imported>Mic92
syntax highlighting; revisit nix shell
Line 11: Line 11:


mkShell {
mkShell {
   buildInputs = with pkgs; [
   nativeBuildInputs = [
     ns-3
     llvmPackages_latest.clang
    clang
     pkg-config
     pkg-config
   ];
   ];
  buildInputs = [ ns-3 ];
}
}
</syntaxhighlight>
</syntaxhighlight>
Line 21: Line 21:
Then we can create our <code>Makefile</code>, do note the LIBS argument, which is the modules required (not all are build by default, might have to override some settings in ns-3).
Then we can create our <code>Makefile</code>, do note the LIBS argument, which is the modules required (not all are build by default, might have to override some settings in ns-3).


<syntaxhighlight>
<syntaxhighlight lang=Make>
# the compiler: gcc for C program, define as g++ for C++
# the compiler: gcc for C program, define as g++ for C++
CC = clang++
CC = clang++

Revision as of 18:07, 28 October 2020

This is for discrete-event network simulator ns-3.

Developing with ns-3

ns-3 heavily relies on way for running simulations (examples, tutorials, etc.), but we can start developing using a simple Makefile and a shell.nix. We will use hello-simulator.cc for this example.

First create a shell.nix, with the requirements to compile our simulation.

with import <nixpkgs> {};

mkShell {
  nativeBuildInputs = [
    llvmPackages_latest.clang
    pkg-config
  ];
  buildInputs = [ ns-3 ];
}

Then we can create our Makefile, do note the LIBS argument, which is the modules required (not all are build by default, might have to override some settings in ns-3).

# the compiler: gcc for C program, define as g++ for C++
CC = clang++

DEBUG=-DNS3_LOG_ENABLE

LIBS = libns3-dev-core-debug

# compiler flags:
#  -g    adds debugging information to the executable file
#  -Wall turns on most, but not all, compiler warnings
CFLAGS  = -g -Wall -Wextra $(shell pkg-config --cflags $(LIBS))
LDFLAGS=$(shell pkg-config --libs $(LIBS))

# the build target executable:
TARGET = hello-simulator

all: $(TARGET)

$(TARGET): $(TARGET).cc
	$(CC) $(DEBUG) $(CFLAGS) $(LDFLAGS) -o $(TARGET) $(TARGET).cc

clean:
	$(RM) $(TARGET)

Then just place hello-simulator.cc in the same directory as the other files, then run the following commands.

$ nix-shell
$ make
$ ./hello-simulator

Please keep in mind our flag when we compile, regarding debug NS3_LOG_ENABLE.