0

I am modifying CPP Arduino library. purpose is to add hostname in user code.

Here is Arduino ethernet library: Arduino Ethernet Library

I am using ethernet.h in my source code.

in ethernet.h there is include statement.

#include "dhcp.h"

and in dhcp.h macro is there.

#define HOST_NAME "MYBOARD"

in dhcp.cpp this macro used

#include "dhcp.h" 
buffer[17] = strlen(HOST_NAME) ; // length of hostname + last 3 bytes of mac address
strcpy((char*)&(buffer[18]), HOST_NAME);

can this macro replaced with variable, such that I can assign value for HOST_NAME is my source code?

example. in my source code

 variable_name ="MYBOARD1"

should pass value to HOST_NAME in dhcp.cpp

I dont know much about c++ , need help with example code.

Thank You

Sudhir

3 Answers 3

1

#define is a preprocessor directive ("macro"), not a variable. There's no way you can modify it at runtime.

One possibility is to replace #define HOST_NAME "MYBOARD" with anything you want. If you can't do this for some reason, search where the HOST_NAME is used and replace it with your custom variable. I can't help you much more without other details.

Sign up to request clarification or add additional context in comments.

Comments

0

you can not change HOST_NAME because it is already defined in your dhcp.h file,

if you want yo change you have to declare it as a global variable instead of #define

More info about #define:

Why use #define instead of a variable

2 Comments

HOST_NAME isn't a constant. It is a #define, meaning the preprocessor simply replaces it by "MYBOARD" wherever it finds it.
Thank You Vishal for mentioning "global variable" I searched about it, and was able to achive the results, I have posted it above.. But I feel this was a dirty way of fix, help me writing in right way.
0

I was able to achive the result

in dhcp.h I replaced

#define HOST_NAME "my-host-name"

with

extern const char  HOST_NAME[10] ;

then in my project source code I used

const char HOST_NAME[] = "MYHOSTONE";

after compiling I ran the code, observed in DHCP server logs, its showing correct host name. I think its dirty trick, I request expers to help me clean up this line.

thanks

3 Comments

Doesn't extern const char HOST_NAME[] ; work as well (or even better, since you won't need to change it when you change the hostname)?
frarugu87 , I did not think that.. it works.. thanks. next issue is in projects where hostname is not required, if this array does not have value.. when compiling shows arror, how to assign some default value if its not initialized(values not assigned)
I don't think you can leave it empty. You can convert it to a pointer, writing extern const char *HOST_NAME; and const char *HOST_NAME = "MYHOSTONE";, so you can initialize it to NULL. Or you can simply assign it to an empty string (i.e. const char HOST_NAME[] = "";, even if this way wastes a byte), or you can try (but I'm not sure if it'll work) with const char HOST_NAME[0]; (in the project source code, not in the .h file)).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.