简体   繁体   English

CPP-更改库中的#define

[英]CPP - Change #define in library

I am modifying CPP Arduino library. 我正在修改CPP Arduino库。 purpose is to add hostname in user code. 目的是在用户代码中添加主机名。

Here is Arduino ethernet library: Arduino Ethernet Library 这是Arduino以太网库: Arduino以太网库

I am using ethernet.h in my source code. 我在源代码中使用ethernet.h。

in ethernet.h there is include statement. 在ethernet.h中,有一个include语句。

#include "dhcp.h"

and in dhcp.h macro is there. 并且在dhcp.h中存在宏。

#define HOST_NAME "MYBOARD"

in dhcp.cpp this macro used 在dhcp.cpp中使用此宏

#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? 这个宏可以替换为变量,以便为我的源代码HOST_NAME赋值吗?

example. 例。 in my source code 在我的源代码中

 variable_name ="MYBOARD1"

should pass value to HOST_NAME in dhcp.cpp 应该将值传递给dhcp.cpp中的HOST_NAME

I dont know much about c++ , need help with example code. 我对c ++不太了解,需要示例代码的帮助。

Thank You 谢谢

Sudhir 苏迪尔

#define is a preprocessor directive ("macro"), not a variable. #define是预处理程序指令(“宏”),不是变量。 There's no way you can modify it at runtime. 您无法在运行时对其进行修改。

One possibility is to replace #define HOST_NAME "MYBOARD" with anything you want. 一种可能是将#define HOST_NAME "MYBOARD"替换为所需的任何内容。 If you can't do this for some reason, search where the HOST_NAME is used and replace it with your custom variable. 如果由于某种原因无法执行此操作,请搜索HOST_NAME的使用位置,并将其替换为自定义变量。 I can't help you much more without other details. 没有其他细节,我无法为您提供更多帮助。

you can not change HOST_NAME because it is already defined in your dhcp.h file, 您无法更改HOST_NAME,因为它已在您的dhcp.h文件中定义,

if you want yo change you have to declare it as a global variable instead of #define 如果要进行更改,则必须将其声明为全局变量而不是#define

More info about #define: 有关#define的更多信息:

Why use #define instead of a variable 为什么使用#define而不是变量

I was able to achive the result 我能够达到结果

in dhcp.h I replaced 在dhcp.h中,我替换了

#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. 编译后,我运行了在DHCP服务器日志中观察到的代码,该代码显示了正确的主机名。 I think its dirty trick, I request expers to help me clean up this line. 我认为这是肮脏的把戏,我要求专家帮助我清理此行。

thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM