Multiple C files project

12 Dec 2010

I have a multiple C files project that compiles under Keil and runs well in mbed.

If I use mbed cloud compiler, I got: Symbol ... multiply defined error

It seems that I don't understand how the compiler deals with C files.

Can mbed compiler compile C files ?

13 Dec 2010

Hi Mercier,

The "multiply defined error" is the linker finding the same symbol (object name) in two different places. That sounds like you've therefore not actually got the same code. In the mbed compiler, you would almost certainly have the mbed library, which includes things like startup code, interrupt vectors etc. If any of your additional code includes those sorts of things, then you might see these problems.

Simon

18 Dec 2010

it seems my problem is including LPC17xx.h

without LPC17xx.H 

//#include "LPC17xx.h"

 LPC_GPIO_TypeDef *GPIO[5] ={LPC_GPIO0, LPC_GPIO1, LPC_GPIO2, LPC_GPIO3, LPC_GPIO4};

 

Compiler output

"Identifier "LPC_GPIO_TypeDef" is undefined (E20)" in file "/jmbedPinsDef.h"

 

with LPC17xx.H 

#include "LPC17xx.h"

  LPC_GPIO_TypeDef *GPIO[5] ={LPC_GPIO0, LPC_GPIO1, LPC_GPIO2, LPC_GPIO3, LPC_GPIO4};

 

Compiler output

" Symbol Line multiply defined (by main.cpp.cpp.LPC1768.o and jmCommands.c.c.LPC1768.o). (EL6200E)

 

How should I interpret information provided by compiler output ?

 

 

 

18 Dec 2010

Where is GPIO[5] defined, in a header? Then you're probably including that header in several cpp files, triggering multiple definition. You should move the definition to one of the cpp files and leave only the declaration in the header.

19 Dec 2010 . Edited: 19 Dec 2010

GPIO[5] is define in the line below.

 LPC_GPIO_TypeDef *GPIO[5] ={LPC_GPIO0, LPC_GPIO1, LPC_GPIO2, LPC_GPIO3, LPC_GPIO4};

  LPC_GPIO_TypeDef, LPC_GPIO0, LPC_GPIO1, LPC_GPIO2, LPC_GPIO3, LPC_GPIO4 are all defined in LPC17xx.h

 This header file description is in mbed library under mbed/LPC1768.

I'm stuck with this, I also have a dial-up connection, so debugging is almost pain.  

Right now my project is running fine under Keil. My proof of concept is done, it was easy to develop with mbed. It is a good prototyping board. My project now can generate complete applications just by selecting modules. All files are generated, even the project file. So after that, I just have to double click on the project file,   Keil uVision loads, I click to compile, 0 warning, 0 error, then I click the load button to program mbed, I reset mbed and voila, I can use my PC application to control mbed.

I will document it and publish it later.

Here are also some other comments about the compiler:

  • case sensitive on filenames
  • can't clean the project, is it done automatically ?
  • Import files one at a time (can't do multiple selections)

 

 

 

 

21 Dec 2010

When I define global variables, I use this:

#ifndef Messagesdef
    #define Messagesdef 1
      
    // variables to enable/disable messages
   uint8_t Help;
   uint8_t Feedback;
   uint8_t Echo;

#endif

It seems, I should do something else because, compiler complains multiple defines of Help, Feedback and Echo. It usually works with other compilers.

When Messagesdef is define once, the compiler should not go into these definitions. Right ?

Could someone at mbed, can check this out ? I have a uploaded a program called jmSwitch for you to see what I'm dealing with. 

 

21 Dec 2010

You should define them once in, say, main.cpp

And then elsewhere in other files do :-

#ifndef Messagesdef
    #define Messagesdef 1
      
    // variables to enable/disable messages
   extern uint8_t Help;
   extern uint8_t Feedback;
   extern uint8_t Echo;

#endif

If you put that in a .h file then ensure you #define Messagesdef 1 in main.cpp (aasuming that's where you define them) before your #include header.

Note the use of extern which tells the compiler they were defined somewhere else and not to redefine them them.

21 Dec 2010

Hi mercier,

Assuming this in a header file, when you compile any source file that includes it, each one will create some global variables (help, etc), so when you come to link, you'll have more than one with the same name.

You only want to generate the code (variables) in one place. Sobfor example:

foo.h

#ifndef Messagesdef
#define Messagesdef 
       
    // variables to enable/disable messages
   extern uint8_t Help;
   extern uint8_t Feedback;
   extern uint8_t Echo;

#endif

foo.cpp

       
    // variables to enable/disable messages
   uint8_t Help;
   uint8_t Feedback;
   uint8_t Echo;

Hope that helps, Simon