4 years, 4 months ago.

Usinig serial usb as a debug // macro for pc.printf

HI, I am trying to make a standard macro for all debug messages as you could do for normal fprint as bellow

(serialdebug.h) file

<<code>>

  1. ifndef SERIALDEBUG_H
  2. define SERIALDEBUG_H
  1. include "mbed.h" Serial pc(SERIAL_TX, SERIAL_RX);
  1. if DEBUG_TEST
  2. define DEBUG_MESSAGE(...) pc.printf(VA_ARGS)
  3. else
  4. define DEBUG_MESSAGE(...)
  5. endif
  1. endif <</code>> and this file will be included in my other files as I use DEBUG_MESSAGE macro to put out a message. But the compiler doesn't like that and say ::

Error: Symbol pc multiply defined (by main.NUCLEO_F030R8.o and error.NUCLEO_F030R8.o). Error: Symbol pc multiply defined (by main.NUCLEO_F030R8.o and error.NUCLEO_F030R8.o). ...etc

It shows the error in all files having the serialdebug.h included. I really don't understand what the problem is? Any help thanks

Actually, there is an instruction in mbed library .. You don't need to implement this function. Look at (debug(...))

posted by Mariwan J. 02 Jan 2017

1 Answer

4 years, 4 months ago.

Please use the <<code>> and <</code>> tags around posted code to keep it readable.

ifndef SERIALDEBUG_H
define SERIALDEBUG_H

include "mbed.h"
Serial pc(SERIAL_TX, SERIAL_RX);

if DEBUG_TEST
define DEBUG_MESSAGE(...) pc.printf(VA_ARGS)
else
define DEBUG_MESSAGE(...)
endif

endif

It looks like you have at least two .cpp files in your project (main.cpp and error.cpp). Both files are including serialdebug.h These two files are compiled separately (resulting in .o files) and they now both have a Serial object named 'pc'. The linker will try to merge both files and detects a conflict: the variable 'pc' shows up twice and that makes it impossible to decide which one you mean when that name is used.

Solution could be to get rid of the variable 'pc' and just call ''printf'' to direct all debug messages to the default stdout. Alternatively, remove the Serial pc declaration from serialdebug.h and declare pc as external variable in all files except for main.cpp.

include "mbed.h"

extern Serial pc;

#include "serialdebug.h"

//rest of code

Accepted Answer

Thank you. But as I said, I use the DEBUG_MESSAGE function in all my other cpp files. not just in main.cpp. I have to define it somewhere and use it in my cpp code files. I think it is the mbed compiler that cannot take care of the code. The code styling is used by other projects (sure not for serial communication, using printf).

posted by Mariwan J. 15 Oct 2016

I think I understand what you are trying to do. So please read what I have written above:

1) Remove the Serial pc declaration from serialdebug.h

2) Declare pc as external variable in all .cpp files that need debugging (except for main.cpp). Like this:

include "mbed.h"
include "code_filename.h"
 
extern Serial pc;
 
#include "serialdebug.h"
 
//rest of code
 

3) Declare pc as variable in main.cpp. Like this:

include "mbed.h"
include "code1_filename.h"
include "code2_filename.h"

Serial pc(SERIAL_TX, SERIAL_RX);

#include "serialdebug.h"
 
//rest of main code
int main() {

} 
posted by Wim Huiskamp 15 Oct 2016