I'm tring to use an websocket librarie that have macro to selec with ethernet librarie it use.
i want to add to that macro selection of Ethernet2.h .
But I got collision of definition:
(x86)\Arduino\libraries\Ethernet\src/Dhcp.cpp:31: multiple definition of `DhcpClass::reset_DHCP_lease()'
libraries\Ethernet2\Dhcp.cpp.o:C:\Users\...\Documents\Arduino\libraries\Ethernet2\src/Dhcp.cpp:32: first defined here
How should I define macro so that include a libraries in sketch define witch one is use inside libraries?
The error indicates that you are including both the Ethernet and Ethernet2 libraries. It doesn't look like that would happen from the code you posted but maybe there is another part of your code that also contains an #include directive for one or the other. It's also not clear where W5500_H_INCLUDED would be defined.
Now what happens is that when Foo.h is included from Sketch.ino SOME_MACRO is defined and so the "do something" code is executed but then Foo.h is included from Foo.cpp SOME_MACRO is not defined and so the "do something else" code is executed. This is a bit confusing because we're used to thinking that the Foo.h code will always be the same.
A useful technique for troubleshooting preprocessor conditionals is to use #warning to get some debug output. Make sure you have File > Preferences > Compiler warnings turned on so that you can see the #warning output.
an example that not compile... with as less code possible to illustrate.
.ino
/////Ethernet W5100
#include <Dhcp.h>
#include <Dns.h>
#include <Ethernet.h>
//#include <EthernetClient.h>
//#include <EthernetServer.h>
////Ethernet2 W5500
/*
#include <Dhcp.h>
#include <Dns.h>
#include <Ethernet2.h>
//#include <EthernetClient.h>
//#include <EthernetServer.h>
*/
#include <testlib.h>
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
testlib.h
#ifndef TESTLIB
#define TESTLIB
#ifdef W5100_H_INCLUDED
#include "Ethernet.h"
#pragma message ( "W5100 network" )
#elif defined W5500_H_INCLUDED
#include "Ethernet2.h"
#pragma message ( "W5100 network" )
else
#pragma message ( "no network" )
#error "No or invalid network selected"
#endif
#include "Arduino.h"
class Testlib {
Testlib();
};
#endif
.cpp
#include "testlib.h"
Testlib::Testlib(){}
But I think it do not work because the only define variable different on the 2 lib is on the util folder in the w5100 and w550.h file that are deeper #included into the lib... maybe I should try to make a define inside each ethernet.h file to see what I got...
Note that the libraries are cached after the first successful compilation so after that you need to change the Tools > Board selection to clear the cache to get warnings from the library again.
W5100_H_INCLUDED is defined in w5100.h but that file is not included from Ethernet.h, Dhcp.h, or Dns.h (I'm not sure why you're including the last two). You'll need to do this:
But, as I explained in #3, then you'll still run into the problem of testlib.h being included by testlib.cpp, which is a separate translation unit from your .ino file, and thus will not have the macros defined. You could resolve that by moving the code from testlib.cpp to testlib.h.
5100_H_INCLUDED is defined in w5100.h but that file is not included from
I didn't notice it was on ethernet1.. it is not on 2... it is only use by socket I think... good point
I'm not sure why you're including the last two
when you do an automatic include via windows menu of ethernet library, it does include it. I ask myself eater if it is needed...
You could resolve that by moving the code from testlib.cpp to testlib.h
did you more think of moving the code from ino to header ?
I think this is what I will do.
[EDIT] the bug of compiler that see multiple def of dhcp and dns and blabla... that was in fact cause by my inclusion of dhcp and dsn on sketch !! good point. remove it make thing easier.
Ah, I forgot about what this thread was about originally. I see now the whole thing with the macros was only an attempted workaround for that and of course that code is completely unnecessary now that the underlying issue is resolved. It definitely turned into an X-Y problem discussion but I think it's good to learn about translation units anyway.
It's not completely clear to me what the issue is now.
Remember that if you move the code to the .h file then it will be in the same translation unit since the #include in the sketch essentially just inserts the contents of the .h file. That's not typically considered best practices but it does allow some things that would not otherwise be possible.
Ethernet, Ethernet2, UIPEthernet all use the Client class. So if you write your library so that the Client object is created by the user in the sketch and then passed to the library via a function then the library will not be dependent on any specific library. That will allow your library to be also used with any other library that uses the Client class, such as WiFi, ESP8266WiFi, EspWiFi, etc.