I need compile some C code to in my self defined module. And my code use malloc function.But I always got this error, undefined symbol `malloc’
relocation R_X86_64_PC32 against undefined symbol `malloc@@GLIBC_2.2.5' can not be used when making a shared object; recompile with -fPIC
I add the “-fPIC” option to get the problem solved.
The following is the codes about my created module.
The first step is to create a folder name “zsy-pacer” under src folder. The zsy-pacer has a folder and a wscript file.
zsy-pacer
├── model
│ ├── pacer.cc
│ ├── pacer.h
│ ├── pacer_sender.c
│ └── pacer_sender.h
└── wscript
pacer.h
#ifndef PACING_H
#define PACING_H
#include "pacer_sender.h"
#include "ns3/timer.h"
#include "ns3/data-rate.h"
namespace ns3
{
class Pacing
{
public:
Pacing();
~Pacing();
void ChangePacingRate(uint64_t bitrate);
void NotifyPacingPerformed (void);
void Start(void);
static void DataSend(void* handler, uint32_t packet_id, int retrans, size_t size);
private:
Timer m_pacingTimer{Timer::REMOVE_ON_DESTROY};
DataRate m_minPacingRate;
DataRate m_curentPacingRate;
pace_sender_t *m_paceSender;
};
}
#endif
pacer.cc
#include"pacer.h"
#include<stdio.h>
#include<string>
#include<sstream>
#include"ns3/log.h"
namespace ns3
{
NS_LOG_COMPONENT_DEFINE ("PacingModel");
/*12000bps=1500*8bit/s*/
Pacing::Pacing():m_minPacingRate("12000bps"),
m_curentPacingRate(m_minPacingRate)
{
m_paceSender=pace_create((void*)this,(pace_send_func)&Pacing::DataSend);
m_pacingTimer.SetFunction(&Pacing::NotifyPacingPerformed,this);
}
Pacing::~Pacing()
{
if(m_paceSender!=nullptr)
{
pace_destroy(m_paceSender);
m_paceSender=nullptr;
}
}
void Pacing::ChangePacingRate(uint64_t bitrate)
{
std::stringstream ss;
ss<<bitrate<<"bps";
std::string s=ss.str();
DataRate newval(s);
NS_LOG_INFO("change pacing rate from"<<m_curentPacingRate<<"to"<<newval);
m_curentPacingRate=newval;
}
void Pacing::NotifyPacingPerformed()
{
pace_send(this->m_paceSender);
}
void Pacing::DataSend(void* handler, uint32_t packet_id, int retrans, size_t size)
{
Pacing *obj=static_cast<Pacing*>(handler);
if(obj->m_pacingTimer.IsExpired ())
{
NS_LOG_DEBUG("pacer timer in expire state"<<"packet seq"<<packet_id);
obj->m_pacingTimer.Schedule(obj->m_curentPacingRate.CalculateBytesTxTime(size));
}
}
void Pacing::Start()
{
m_pacingTimer.Schedule();
}
}
pacer_sender.h
#ifndef __pace_sender_h_
#define __pace_sender_h_
#include<stdint.h>
#include<stddef.h>
#include <stdlib.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef void(*pace_send_func)(void* handler, uint32_t packet_id, int retrans, size_t size);
typedef struct
{
void* handler;
pace_send_func send_cb;
}pace_sender_t;
pace_sender_t* pace_create(void* handler, pace_send_func send_cb);
void pace_destroy(pace_sender_t* pace);
int pace_send(pace_sender_t* pace);
#ifdef __cplusplus
}
#endif
#endif
pacer_sender.h
#include"pacer_sender.h"
pace_sender_t* pace_create(void* handler, pace_send_func send_cb)
{
pace_sender_t *sender=(pace_sender_t*)malloc(sizeof(pace_sender_t));
sender->handler=handler;
sender->send_cb=send_cb;
return sender;
}
void pace_destroy(pace_sender_t* pace)
{
free(pace);
}
static uint32_t seq=0;
int pace_send(pace_sender_t* pace)
{
if(pace->send_cb)
{
pace->send_cb(pace->handler,seq,0,1500);
seq++;
}
return 0;
}
wscript
## -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
def build(bld):
pacer = bld.create_ns3_module('zsy-pacer', ['core','network'])
pacer.source = [
'model/pacer.cc',
'model/pacer_sender.c',
]
headers = bld(features='ns3header')
headers.module = 'zsy-pacer'
headers.source = [
'model/pacer.h',
'model/pacer_sender.h',
]
pacer.env.append_value("CFLAGS","-fPIC")
And the test Code in folder Scratch:
pacer_example.cc
#include "ns3/core-module.h"
#include "ns3/zsy-pacer-module.h"
NS_LOG_COMPONENT_DEFINE("PACER_EXAMPLE");
using namespace ns3;
using namespace std;
static double simDuration=10;
int main(int argc, char *argv[])
{
LogComponentEnable("PACER_EXAMPLE", LOG_LEVEL_ALL);
LogComponentEnable("PacingModel", LOG_LEVEL_ALL);
Pacing pacer;
//pacer.ChangePacingRate(2*1500*8);
pacer.Start();
Simulator::Stop (Seconds(simDuration + 10.0));
Simulator::Run ();
Simulator::Destroy();
}
wscript
## -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
def build(bld):
obj = bld.create_ns3_program('pacer_example',
['core','zsy-pacer'])
obj.source = 'pacer_example.cc'
[1]Compile and link NS3 program with external library
[2]Mixing c code in NS3