How to link static library in ns3? Previously, in order to link static library in ns3, I built all the ns3 lib as static as instructed by[1], and got quite large binary programs. After that, I give up the try to link any static to ns3. Instead, I choose dynamic library. But in some situation, a static library should be chosen first. In blog 2], the author gave some hint on this question. And here, I will release an example.
First, a simple static library is generated with cmake.
foo.h
#ifndef FOO_H_
#define FOO_H_
#include <string>
void foo_print(std::string content);
#endif /* FOO_H_ */
foo.cc
#include "foo.h"
#include <iostream>
void foo_print(std::string content){
std::cout<<content<<std::endl;
}
CMakeLists.txt
PROJECT(project)
cmake_minimum_required(VERSION 2.6)
SET(CMAKE_BUILD_TYPE "Debug")
SET(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g2 -ggdb")
SET(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
include_directories(${CMAKE_SOURCE_DIR}/)
set(CMAKE_CXX_FLAGS "-fPIC")
set(CMAKE_C_FLAGS "-fPIC")
set(foo_FILES
foo.cc
)
add_library(foo STATIC ${foo_FILES})
And the compiling option “-fPIC” must be added to generate Position-Independent Code. The lib is put under the path “/home/zsy/MyTest/foo”.
&esmp;Second, Create a new module in ns3 under src file and I create a module named mp3.
wscript
def build(bld):
mp3= bld.create_ns3_module('mp3')
mp3.env.append_value("CXXFLAGS", "-I/home/zsy/MyTest/foo")
mp3.env.append_value("LINKFLAGS", ["-L/home/zsy/MyTest/foo/build"])
mp3.env.append_value("LIB", ["foo"])
mp3.source = [
'model/mysrc.cc'
]
headers = bld(features='ns3header')
headers.module = 'mp3'
headers.source = [
'model/mysrc.h',
]
model/mysrc.h
#include <string>
//#include <foo.h> fatal error: foo.h: No such file or directory
// move it to the source file
namespace ns3{
class MySrc{
public:
void Print(std::string name);
};
}
model/mysrc.cc
#include "ns3/mysrc.h"
#include <foo.h>
namespace ns3{
void MySrc::Print(std::string name){
foo_print(name);
}
}
Finally, add a test file under scratch.
scratch/link-static.cc
#include "ns3/mysrc.h"
using namespace ns3;
int main(){
MySrc src;
std::string name("foo");
src.Print(name);
}
But in this method, the external must be included in the cc source file(model/mysrc.cc), which will introduce some inconvenience when we want use external class. So, I work out a way to get around the problem. Apply the inner class forward declaration[3].
The full code can get be downloaded at [4].
The method to introduce external library and functions by inner class is also not quite convenience. So introduce the head path of the static library in environment variable can total get this problem perfectly solved[5].
Add the following to /etc/profile
CPLUS_INCLUDE_PATH=$CPLUS_INCLUDE_PATH:/usr/include/libxml2:/MyLib
export CPLUS_INCLUDE_PATH
When building ns3, input “source /etc/profile” to make it work.
Add defination flag
Some third party may contains some predefined macro. such flag can be enabled in wscript. For example, I use the libwebrtc, and this lib must enable the WEBRTC_POSIX flag.
def build(bld):
module= bld.create_ns3_module('ex-webrtc')
module.env.append_value("CXXFLAGS", "-I/home/zsy/webrtc2/webrtc/src/")
module.env.append_value("LINKFLAGS", ["-L/home/zsy/webrtc2/webrtc/src/out/m84/obj/"])
module.env.append_value("LIB", ["webrtc"])
module.env.append_value('CXXFLAGS', '-DWEBRTC_POSIX')
module.source = [
'model/webrtc-header.cc',
'model/webrtclog.cc',
'model/webrtc-util.cc',
'test/rtp_header_parser.cc',
]
headers = bld(features='ns3header')
headers.module = 'ex-webrtc'
headers.source = [
'model/webrtc-defines.h',
'model/webrtc-header.h',
'model/webrtclog.h',
'model/webrtc-util.h',
]
reference:
[1]Making use of external libraries in NS3 https://jiaziyi.com/making-use-of-external-libraries-in-ns3/
[2]Compile and link NS3 program with external library
[3] https://stackoverflow.com/questions/1021793/how-do-i-forward-declare-an-inner-class
[4] https://github.com/SoonyangZhang/ns3-static-library
[5]Linux添加头文件路径—INCLUDE_PATH https://blog.csdn.net/kxalpah/article/details/53215264