Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Store machine hostname at compile time

Name: troll 2010-10-24 15:26

Hi g33kz!
I saw some apps, which storred machine hostname at build time. I am coding one project in C, and I want to store machine hostname, where the binary was compiled. I cannot find it. How can I do it? Is there some preprocessor macro or what? I am using gcc.
Thanks

Name: Anonymous 2010-10-24 15:30

__HOSTNAME__

Name: troll 2010-10-24 15:31

test.c: In function 'main':
test.c:4: error: '__HOSTNAME__' undeclared (first use in this function)
test.c:4: error: (Each undeclared identifier is reported only once
test.c:4: error: for each function it appears in.)

Name: Anonymous 2010-10-24 15:36

Why would you want to do that?
One very simple solution would be to actually use code-at-compile-time like provided by Lisp macros, however since you want to do it in C, you can either depend on implementation specific defines or just use your own post-processor (perl, M4, whatever language, even C would work).

Name: >>4 2010-10-24 15:37

s/post-/pre-//

Name: >>4 2010-10-24 15:40

If using your own pre-processor is overkill, you can always just write a little C application which writes a tiny header file "host-information.h" which is then included where you need these values. You'd just have to make sure to compile and run this application before the rest of the code, however you can easily do that with whatever makefile/build system you have going.

Name: Anonymous 2010-10-24 15:43

What an utterly stupid thing to do.
But if you insist, man make.

Name: Anonymous 2010-10-24 15:46

>>7
this

Name: troll 2010-10-24 16:03

There is nothing in manpage of make. It is not so stupd thing as it seems to be....

Name: Anonymous 2010-10-24 16:11

#include "/proc/sys/kernel/hostname"

Name: Anonymous 2010-10-24 20:22

>>9
shit: shit.c
    $(CC) -o $@ $(CFLAGS) -DHOSTNAME=`hostname` $<

Name: Anonymous 2010-10-24 20:56

>>11
YOU HELPED HIM!!

Don't change these.
Name: Email:
Entire Thread Thread List