Jump to content
lee_timmy

Errors about multiple definition of a function ‏when compiling

Recommended Posts

Hi, all

I tried to implement the time domain Gaussian random noise in page 93 of AMS user manual.

I made this as an sub-module of the BASK modulation/demodulation system.

However, the compilation gives me errors:

Gnoise.o: In function `gauss_rand(double)':

Gnoise.cpp:(.text+0x60): multiple definition of `gauss_rand(double)'

BASK.o:BASK.cpp:(.text+0x60): first defined here

/tmp/ccRJX6KO.o: In function `gauss_rand(double)':

test_BASK.cpp:(.text+0x60): multiple definition of `gauss_rand(double)'

BASK.o:BASK.cpp:(.text+0x60): first defined here

collect2: ld returned 1 exit status

make: *** [test_BASK] Error 1

It says the the C++ function gauss_rand(double) is defined in multiple places. But I'm sure that's Gnoise.cpp is the only file where I use and define this function.

How can SystemC/AMS call a C++ function in a module without getting this error?

Thank you!

Tim

Share this post


Link to post
Share on other sites

Dear Timmy,

Most possibly you are missing some compiler guards. Guard your code using something like as follows :

# ifndef GAUSSIAN_RANDOM_H_
# define GAUSSIAN_RANDOM_H_

// your declaration goes here

# endif

Regards,

Sumit

Share this post


Link to post
Share on other sites

Dear Timmy,

Most possibly you are missing some compiler guards. Guard your code using something like as follows :

# ifndef GAUSSIAN_RANDOM_H_
# define GAUSSIAN_RANDOM_H_

// your declaration goes here

# endif

Regards,

Sumit

Hi Sumit,

header guards won't necessarily help. They stop two header files being included sequentially - but if you have a function *body* in the header, and include that header in two independent files, you'll still get multiple definitions at link time.

My guess is that Timmy is #including a gnoise.cpp.

In that case the fix is to make a gnoise.h with the function prototype, with your header guards of course, and include the .h not the .cpp,

regards

Alan

Share this post


Link to post
Share on other sites

Thanks, Alan. You are correct.

I separated my Gnoise.cpp to a Gnoise.h with the function prototype, and Gnoise.cpp only having the function of sc_processing. The C++ function "gauss_rand(double)" is called in Gnoise.cpp.

In this way, Gnoise.h can be included in its top module. The compilation is passed and simulation seems correct.

Thank you all!

Tim

Hi Sumit, header guards won't necessarily help. They stop two header files being included sequentially - but if you have a function *body* in the header, and include that header in two independent files, you'll still get multiple definitions at link time. My guess is that Timmy is #including a gnoise.cpp. In that case the fix is to make a gnoise.h with the function prototype, with your header guards of course, and include the .h not the .cpp, regards Alan

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...