error using NS_LOG_COMPONENT macro in a .h file (redefinition of ‘ns3::LogComponent ns3::g_log’)

331 views
Skip to first unread message

Francisco Eduardo Balart Sanchez

unread,
May 30, 2017, 8:05:57 PM5/30/17
to ns-3-users
Good day: 

In my-new-component.h file I declared a new component following the guidelines shown at:  


namespace ns3
{
NS_LOG_COMPONENT_DEFINE ("mynewcomponent");

when i compile with ./waf i get the following

[1797/1916] Compiling src/wifi/model/FilethatincludestheMynewcomponentheader.cc
In file included from ./ns3/my-new-component.h:33:0,
                 from ../src/wifi/model/HeaderFilethatincludestheMynewcomponentheader.cc:32,
                 from ../src/wifi/model/FilethatincludestheMynewcomponentheader.cc:25:
./ns3/log.h:202:28: error: redefinition of ‘ns3::LogComponent ns3::g_log’
   static ns3::LogComponent g_log = ns3::LogComponent (name, __FILE__)
                            ^
../src/wifi/model/FilethatincludestheMynewcomponentheader.cc:33:1: note: in expansion of macro ‘NS_LOG_COMPONENT_DEFINE’
 NS_LOG_COMPONENT_DEFINE ("mynewcomponent");
 ^
./ns3/log.h:202:28: error: ‘ns3::LogComponent ns3::g_log’ previously declared here
   static ns3::LogComponent g_log = ns3::LogComponent (name, __FILE__)
                            ^
./ns3/essoa-ESSOARole.h:38:1: note: in expansion of macro ‘NS_LOG_COMPONENT_DEFINE’
 NS_LOG_COMPONENT_DEFINE ("mynewcomponent");



It seems that this happened before at:

and

I´m creating all my*.h and *.cc files inside the wifi module so i already included the *.h and *.cc files in the 
wscript at the ns-3.25/src/wifi directory

Not sure what is wrong here or the logging is only for *.cc files?

Rediet

unread,
May 31, 2017, 3:59:52 AM5/31/17
to ns-3-users
Hello,

It seems that by placing the same macro in .h and .cc files, you're redefining the underlying variable. Why not use the NS_LOG_COMPONENT_DEFINE only in .cc files as done throughout all the ns-3 codebase (unless, of course you have inline definitions in the .h that you want to log) ?

Rediet

Francisco Eduardo Balart Sanchez

unread,
May 31, 2017, 5:32:23 PM5/31/17
to ns-3-users
I´m not repeating the same component name, meaning in the  foo1.h file i define
NS_LOG_COMPONENT_DEFINE(foo1)
Then i have a foo2.h and foo2.cc files
In the foo2.h i define it own component
NS_LOG_COMPONENT_DEFINE(foo2)

So i´m not repeating the same component definition, just using the NS_LOG_COMPONENT_DEFINE macro

So for the moment ok, i will comment it but it should be defined as a bug, or in the guidelines


Re-edit and specify that only do it in *.cc files

Regards 

Rediet

unread,
Jun 1, 2017, 4:35:57 AM6/1/17
to ns-3-users
Hello,
If I'm understanding the macro right, it's defining a variable g_log. However, if foo1.h is included in foo2.h (or, foo2.cc includes both of them), you'll end up defining the same static variable twice. Maybe that's why it's preferable to define it in .cc only.
As a matter of fact, I had encountered a similar problem while trying to have different log components within the same .cc file (one per class) but realized that there should be only one definition per (.cc) file.
I'll let @Tommasso or @Konstantinos confirm this and modify, if need be, the manual as per your suggestion...
Best regards

Francisco Eduardo Balart Sanchez

unread,
Jun 1, 2017, 2:31:55 PM6/1/17
to ns-3-users
Thanks for the quick response and follow up

Eduardo

pdbarnes

unread,
Jun 5, 2017, 9:53:55 PM6/5/17
to ns-3-users
Stefano just finished a patch for this bug:
https://codereview.appspot.com/320790043/

You're invited to test it on your code. Please report any issues asap since it will be pushed very soon.

Peter

Reply all
Reply to author
Forward
0 new messages