declare variable in library namespace bug

Hi.

I just try to add a namespace inside a library. I'm able to set function prototype but the compiler bug when I try to declare a variable. it is on a blank sketch:

.ino

#include <snippets.h>

void setup() {
using namespace snippets;
}

void loop() {


}

.h

#ifndef snippets_H
#define snippets_H

#if (ARDUINO >= 100)
    #include "Arduino.h"
#else
    #include "WProgram.h"
#endif


namespace snippets{
	void testNameSpace();
  	void test2();
  	int foo;
}



#endif

the compiler gave me this error:

libraries\snippets\snippets.cpp.o: In function `testfunction()':

C:\Users\o_for\Documents\Arduino\libraries\snippets/snippets.cpp:19: multiple definition of `snippets::x'

sketch\snippets_namespace.ino.cpp.o:C:\Users\o_for\Documents\Arduino\snippets_namespace/snippets_namespace.ino:15: first defined here

collect2.exe: error: ld returned 1 exit status

exit status 1
Error compiling.

Thanks

Where is 'testfunction()'?

That is where it is saying the error is.

There is also no snippets::x in your code...

However, the declaration int foo will be defined in both snippets.cpp and the ino file. You need to use an 'extern' variable, and define it in one of the files.

Many examples of this around to help you out.

ho sorry for my mess. for the function, it was an arthefact of test I was trying... and I had replace x by foo for the question in forum and just copy paste the error. that was confusing... I remove everything and just kept the integer. And yes it left me with double defining of foo in .ino file.

That's the part I don't understand.

How or why it is declaring in ino when it is on namespace? I saw many tutorial and the declare variable inside namespace without problem...

A variable declared inside a custom namespace is no different to a variable declared in the global namespace: You can only have one definition.

Namespaces imply internal linkage (it must be a unique symbol to the source), whereas you need extern (external linkage, a common symbol to many source files).
Just make foo in the header extern. Then in either the cpp or ino, initialize foo to a default value.

With fundamental types in a header (and objects with a default constructor), you can assume that a declaration is a definition unless extern is specified. Thus each file that includes the header is ultimately defining the variable. When included more than once, you have multiple definitions.

Good thank's.

conclusion for other people.. :stuck_out_tongue:

.h

#ifndef snippets_H
#define snippets_H

#if (ARDUINO >= 100)
    #include "Arduino.h"
#else
    #include "WProgram.h"
#endif


namespace snippets{
 void testNameSpace();
 extern int x;
}



#endif

.cpp

#if (ARDUINO >= 100)
  #include "Arduino.h"
#else
  #include "WProgram.h"
#endif
#include "snippets.h"



  void snippets::testNameSpace(){
 //do something
}

  int snippets::x = 2;

.ino

#include <snippets.h>

void setup() {
using namespace snippets;
int z = x;
}

void loop() {


}

Don't put your namespace in the .ino file. See How to avoid the quirks of the IDE sketch file pre-preprocessing.

Hi Nick.

I tried to leave the .ino file empty... it doesn't worked. Still have the same error of double defining...

//.h

#ifndef snippets_H
#define snippets_H

#if (ARDUINO >= 100)
    #include "Arduino.h"
#else
    #include "WProgram.h"
#endif


namespace snippets{
	void testNameSpace();
  	int x = 2;
}



#endif

//.ino empty or with library include; same result.

//snippets_ext.cpp


#include <arduino.h>
#include <snippets.h>

void setup() {
//using namespace snippets;
//int z = x;
}

void loop() {


}

It worked OK for me - IDE 1.6.7.

With the .info file empty:


snippets_ext.cpp

#include <Arduino.h>
#include "snippets.h"

using namespace snippets;

void setup() {
int z = x;
}

void loop() {

testNameSpace (); 
}

void snippets::testNameSpace ()
  {
  return;
  }

snippets.h

#ifndef snippets_H
#define snippets_H

#include <Arduino.h>

namespace snippets 
{
void testNameSpace();
int x = 2;
}

#endif

Result:

Build options changed, rebuilding all

Sketch uses 472 bytes (1%) of program storage space. Maximum is 32,256 bytes.
Global variables use 9 bytes (0%) of dynamic memory, leaving 2,039 bytes for local variables. Maximum is 2,048 bytes.
//using namespace snippets;

Please post your actual code, not something that roughly looks like it.

that's it.. it's a blank sketch... the bug append I was beginning my project. let me clear it a little bit. I remove any muted comment.
It is a the start I've got problem...

in snippets.h file

#ifndef snippets_H
#define snippets_H

#if (ARDUINO >= 100)
    #include "Arduino.h"
#else
    #include "WProgram.h"
#endif


namespace snippets{
   int x = 2;                  /////////// if I mute this definition, the compiler work
}


#endif

un snippets.ccp file

#if (ARDUINO >= 100)
  #include "Arduino.h"
#else
  #include "WProgram.h"
#endif
#include "snippets.h"

in snippets_example.ino file

#include <snippets.h>

in snippets_ext.cpp (that's the tab I add like you link sujested to)

#include <arduino.h>
#include <snippets.h>

void setup() {
}

void loop() {


}

I doesn't have anything else wrote yet. and the compiler give me this:

sketch\snippets_namespace.ino.cpp.o:(.data._ZN8snippets1xE+0x0): multiple definition of `snippets::x'

sketch\snippets_ext.cpp.o:(.data._ZN8snippets1xE+0x0): first defined here

libraries\snippets\snippets.cpp.o:(.data._ZN8snippets1xE+0x0): multiple definition of `snippets::x'

sketch\snippets_ext.cpp.o:(.data._ZN8snippets1xE+0x0): first defined here

collect2.exe: error: ld returned 1 exit status

exit status 1
Error compiling.

oh and ide 1.6.7 to

First error with your setup:

/tmp/arduino_dac540b1228fcf1c2a5e445bdfb5b833/sketch_jan19b.ino:1:22: fatal error: snippets.h: No such file or directory
 #include <snippets.h>
                      ^
compilation terminated.
exit status 1
Error compiling.

It has to be:

 #include "snippets.h"

Second error:

sketch/snippets_ext.cpp:2:21: fatal error: arduino.h: No such file or directory
 #include <arduino.h>
                     ^
compilation terminated.
exit status 1
Error compiling.

It has to be:

 #include <Arduino.h>

sketch/snippets.cpp.o:(.data._ZN8snippets1xE+0x0): multiple definition of `snippets::x'
sketch/sketch_jan19b.ino.cpp.o:(.data._ZN8snippets1xE+0x0): first defined here
sketch/snippets_ext.cpp.o:(.data._ZN8snippets1xE+0x0): multiple definition of `snippets::x'
sketch/sketch_jan19b.ino.cpp.o:(.data._ZN8snippets1xE+0x0): first defined here
collect2: error: ld returned 1 exit status
exit status 1
Error compiling

Don't define variables in a .h file. You included it in multiple places. You define variables in .cpp files.

This is absolutely nothing to do with namespaces. If you define a variable in a .h file, and include that in multiple places, you will get a duplicate definition error. You can put function prototypes in a .h file.

ok. so is there a way I can use a namespace inside a library ?

What are you asking exactly? Of course. My code demonstrated it. Don't define variables inside a .h file. That applies even if you don't use a namespace.

I want to have some function inside a library but without a class for some function shortcut that I often use.
I want to regroup them in a namespace to be free of the "instance.function" in a class...

I don't know if I'm enough clear... :stuck_out_tongue:

Your example had a variable. My example had a function. Try to make an example that uses a function. Put the function prototype in the .h file.

I want to have some function inside a library ...

So why did your example in reply #9 not have a function in the namespace?

ok.

make that function.

int funcDelay(int delay) {
  int delayOutput =  (delay - (millis() - lastMillis));
  delayOutput = constrain(delayOutput, 0, delay);
  lastMillis = millis();
  return delayOutput;
}

this need those prototypes:

funcDelay(int delay);
int lastMillis;