Ubuntu 20.04 LTS compile and install ns-3.40

Tip: After the article is written, the table of contents can be automatically generated. For how to generate it, please refer to the help document on the right.

Article directory

  • 1. Source code download
  • 2. Configure ns-3
    • 2.1 Basic dependencies
    • 2.2 Enabling the module Taking `BRITE Integration` as an example
    • 2.3 Netanim compilation and Qt dependencies
  • 4. Compile
  • 5. Test
  • 6. Configure VsCode development environment
  • 7. clangd plugin

1. Source code download

GitLab download address: nsnam/ns-3-allinone · GitLab
Official website download address: nsnam/ns-3-allinone · release

Download and unzip:

wget https://www.nsnam.org/releases/ns-allinone-3.40.tar.bz2
tar -jxvf ns-allinone-3.40.tar.bz2 ns-allinone-3.40

2. Configure ns-3

Reference: A method to install and use ns-3.38 on Windows – Zhihu (zhihu.com)

When configuring ns-3, a Summary will be generated to describe the compilable modules Please add an image description
In the figure, OFF (not requested) means that the module is not enabled, OFF (missing dependency) means that the module has incomplete dependencies and is not enabled. A method to install and use ns-3.38 on Windows – Zhihu (zhihu.com) The dependencies of each module and the method of enabling the module are sorted out.

2.1 Basic dependencies

Here is a simple transfer:

# Required
sudo apt install g++ python3 cmake ninja-build git -y
sudo apt install python3-pip -y

# recommend
sudo apt install ccache -y
sudo apt install clang-format clang-tidy -y
sudo apt install gdb valgrind -y
python3 -m pip install --user cppyy
sudo apt install mercurial -y
sudo apt install cmake-format -y

# optional
sudo apt install tcpdump wireshark -y
sudo apt install sqlite sqlite3 libsqlite3-dev -y
sudo apt install qtbase5-dev qtchooser qt5-qmake qtbase5-dev-tools -y
sudo apt install openmpi-bin openmpi-common openmpi-doc libopenmpi-dev -y
sudo apt install doxygen graphviz imagemagick -y
sudo apt install python3-sphinx dia imagemagick texlive dvipng latexmk texlive-extra-utils texlive-latex-extra texlive-font-utils -y
sudo apt install libeigen3-dev -y
sudo apt install gsl-bin libgsl-dev libgslcblas0 -y
sudo apt install libxml2 libxml2-dev -y
sudo apt install libgtk-3-dev -y
sudo apt install lxc-utils lxc-templates vtun uml-utilities ebtables bridge-utils -y
sudo apt install libxml2 libxml2-dev libboost-all-dev -y

sudo apt install gir1.2-goocanvas-2.0 python3-gi python3-gi-cairo python3-pygraphviz gir1.2-gtk-3.0 ipython3 -y

2.2 Enabling module Taking BRITE Integration as an example

Return to the upper-level folder of ns-3.40 and execute

hg clone http://code.nsnam.org/BRITE
cdBRITE
make
cd..
# Configure ns-3
cdns-3.40
/ns3 configure --with-brite=/home/usrname/path/ns-allinone-3.40/BRITE

2.3 Netanim compilation and Qt dependency

netanim is a network visualization program based on Qt. When using ./build.py to compile, netanim will be compiled first by default. If this component is not used, , you can directly enter ns-3.40 and use ./ns3 build to compile. The Qt version used by developers is higher than the Qt version installed by default in Ubuntu apt. The new version of the functions called there will cause compilation failure, such as error: 'class QFontMetrics' has no member named 'horizontalAdvance', which This is caused by the width function being replaced by horizontalAdvance.

Here are two solutions:

  1. Modify the source code and replace the content replaced by the higher version of Qt. Refer to ns3.39 compile time errors and solutions_including netanim-3.109 (NetAnim)_Mr_liu_666’s blog-CSDN blog.
  2. To upgrade the Qt version (recommended), you can refer to my other blog Compiling source code to install Qt 5.15.

4. Compile

Enter the unzipped directory and view the compilation instructions in README.md

  1. Official website download:

    ./build.py --enable-examples --enable-tests
    
  2. GitLab download:

    ./download.py
    ./build.py --enable-examples --enable-tests
    

5. Test

  1. ./ns3 run hello-simulator
    Hello Simulator
    
  2. ./ns3 run first
    At time + 2s client sent 1024 bytes to 10.1.1.2 port 9
    At time + 2.00369s server received 1024 bytes from 10.1.1.1 port 49153
    At time + 2.00369s server sent 1024 bytes to 10.1.1.1 port 49153
    At time + 2.00737s client received 1024 bytes from 10.1.1.2 port 9
    

Note that the running method of the old version is ./waf --run hello-simulator, and the running method of the new version is ./ns3 run.

In addition, the running simulation file is stored in the scratch folder, and the content under this file is tested (run according to the file structure under build/scratch):

  1. ./ns3 run scratch/scratch-simulator
    Scratch Simulator
    
  2. ./ns3 run scratch/subdir/scratch-subdir
    Scratch Subdir
    

6. Configure VsCode development environment

Reference: [ns-3] VS Code development environment configuration_ns3 environment configuration_Dabenniu@’s blog-CSDN blog
You can refer to the link above to configure the Vscode environment. During the configuration process, the Ctrl + Shift + P navigation failure occurs. You can create the corresponding files directly in the .vscode folder, and the plug-in can also recognize these configurations.

This is my configuration:

  1. c_cpp_properties.json (I think the includePath here should be consistent with the content in the build folder. Different versions may be different. compileCommands is to configure clangd Used by plug-ins, will be mentioned later)
    {<!-- -->
        "configurations": [
            {<!-- -->
                "name": "Linux",
                "includePath": [
                    "${workspaceFolder}/build/include/**",
                    "${workspaceFolder}/build/include/ns3/**"
                ],
                "defines": [],
                "compilerPath": "/usr/bin/gcc",
                "cStandard": "gnu11",
                "cppStandard": "gnu + + 14",
                "intelliSenseMode": "linux-gcc-x64",
                "compileCommands": "${workspaceFolder}/compile_commands.json"
            }
        ],
        "version": 4
    }
    
  2. launch.json Maybe it’s because I installed gdb and lldb. There are two sets of launch and testrunner. You can just copy the first one in configurations, here Please pay attention to changing ns3-dev to ns3.40, and then the following ${input:buildType} must be the same as the compile-time build -profile corresponding, in short, it needs to be aligned with the program name in the build/scratch folder, such as ns3.40-scratch-simulator-debug, We need to change “program” to "${workspaceFolder}/build/${relativeFileDirname}/ns3.40-${fileBasenameNoExtension}-debug".
    {<!-- -->
        // Use IntelliSense to learn about possible attributes.
        // Hover to view descriptions of existing attributes.
        // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
        "version": "0.2.0",
        "configurations": [
            {<!-- -->
                "name": "(gdb) Launch from scratch",
                "type": "cppdbg",
                "request": "launch",
                "program": "${workspaceFolder}/build/${relativeFileDirname}/ns3.40-${fileBasenameNoExtension}-${input:buildType}",
                "args": [],
                "stopAtEntry": false,
                "cwd": "${workspaceFolder}",
                "preLaunchTask": "Build",
                "environment": [
                    {<!-- -->
                        "name": "LD_LIBRARY_PATH",
                        "value": "${workspaceFolder}/build/lib/"
                    }
                ],
                "externalConsole": false,
                "MIMode": "gdb",
                "setupCommands": [
                    {<!-- -->
                        "description": "Enable pretty-printing for gdb",
                        "text": "-enable-pretty-printing",
                        "ignoreFailures": true
                    }
                ]
            },
            {<!-- -->
                "name": "(lldb) Launch from scratch",
                "type": "cppdbg",
                "request": "launch",
                "program": "${workspaceFolder}/build/${relativeFileDirname}/ns3.40-${fileBasenameNoExtension}-${input:buildType}",
                "args": [],
                "stopAtEntry": false,
                "cwd": "${workspaceFolder}",
                "preLaunchTask": "Build",
                "environment": [
                    {<!-- -->
                        "name": "LD_LIBRARY_PATH",
                        "value": "${workspaceFolder}/build/lib/"
                    }
                ],
                "externalConsole": false,
                "MIMode": "lldb"
            },
            {<!-- -->
                "name": "(gdb) Launch testrunner",
                "type": "cppdbg",
                "request": "launch",
                "program": "${workspaceFolder}/build/utils/ns3.40-test-runner-${input:buildType}",
                "args": [
                    "--suite=${selectedText}"
                ],
                "stopAtEntry": false,
                "cwd": "${workspaceFolder}",
                "preLaunchTask": "Build",
                "environment": [
                    {<!-- -->
                        "name": "LD_LIBRARY_PATH",
                        "value": "${workspaceFolder}/build/lib/"
                    }
                ],
                "externalConsole": false,
                "MIMode": "gdb",
                "setupCommands": [
                    {<!-- -->
                        "description": "Enable pretty-printing for gdb",
                        "text": "-enable-pretty-printing",
                        "ignoreFailures": true
                    }
                ]
            },
            {<!-- -->
                "name": "(lldb) Launch testrunner",
                "type": "cppdbg",
                "request": "launch",
                "program": "${workspaceFolder}/build/utils/ns3.40-test-runner-${input:buildType}",
                "args": [
                    "--suite=${selectedText}"
                ],
                "stopAtEntry": false,
                "cwd": "${workspaceFolder}",
                "preLaunchTask": "Build",
                "environment": [
                    {<!-- -->
                        "name": "LD_LIBRARY_PATH",
                        "value": "${workspaceFolder}/build/lib/"
                    }
                ],
                "externalConsole": false,
                "MIMode": "lldb"
            }
        ],
        "inputs": [
            {<!-- -->
                "type": "pickString",
                "id": "buildType",
                "description": "What is the build option?",
                "options": [
                    "debug",
                    "default",
                    "optimized",
                    "release",
                    "minsizerel"
                ],
                "default": "default"
            }
        ]
    }
    
  3. tasks.json
    {<!-- -->
        // See https://go.microsoft.com/fwlink/?LinkId=733558
        // for the documentation about the tasks.json format
        "version": "2.0.0",
        "tasks": [
            {<!-- -->
                "label": "Build",
                "type": "shell",
                "command": "./ns3",
                "group": {<!-- -->
                    "kind": "build",
                    "isDefault": true
                }
            },
            {<!-- -->
                "label": "Run tests",
                "type": "shell",
                "command": "./test.py",
                "group": {<!-- -->
                    "kind": "test",
                    "isDefault": true
                }
            }
        ]
    }
    

Finally: Ctrl + Shift + B to compile, F5 to debug

7. clangd plug-in

The clangd plug-in will establish an index database based on the compile_commands.json generated during compilation, and then quickly complete jumps and completions. The actual measurement is better than intelligence Plugins are much faster.

# Install the plug-in that generates compile_commands.json
sudo apt install bear
# Configure ns3. The build-profile here can choose debug or release. Later, only the instance and test files are compiled together.
./ns3 configure --build-profile=release --enable-examples --enable-tests
# Generate compile_commands.json
bear ./ns3 build

Then install the clangd plug-in and download clangd-linux-16.0.2.zip

unzip clangd-linux-16.0.2.zip
cd clangd_16.0.2/bin/ # There is clangd in this folder
pwd # /home/zhangbochun/tools/clangd_16.0.2/bin

Arguments to configure clangd plug-in: Please add image description
Configure clangd path:

Clangd: Path
/home/zhangbochun/tools/clangd/clangd_16.0.2/bin/clangd

Remember to set “compileCommands” in c_cpp_properties.json to locate compile_commands.json.

After completing the configuration, the clangd plug-in will automatically activate the database creation and store it in the .cache folder. After the creation is completed, the jump can be completed smoothly: Please add image description