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:
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.
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.
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.
#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.
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
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.
/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.
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...