LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-23-2010, 12:19 AM   #1
orrt
LQ Newbie
 
Registered: Mar 2010
Posts: 4

Rep: Reputation: 0
C++ on Ubuntu - #defines aren't working


Hi everyone,

I'm writing a project in which I've defined several variables in a header file, included that header file in the main cpp file and tried to print the values of the defines from the main cpp file. When I do try to print them they get printed if I've run a function which also included the header file. I've shortened the code but it'll still work.


Please find the 3 files below:

Code:
------------------------- main.cpp -------------------------

#include "LinuxHardwareInfo.h"
#include <stdio.h>




int main (int argc, char* argv[]) 
{

printf("\n\n\n\n------------AA----HDD_SERIAL--AA---->> %s", HDD_SERIAL);
printf("\n\n\n\n----A-- ADDRESS OF HDD_SERIAL-A-->> %p\n", HDD_SERIAL );

	createStorageFiles();

printf("\n\n\n\n--------BB--HDD_SERIAL----BB--->> %s", HDD_SERIAL);
printf("\n\n\n\n----B- ADDRESS OF HDD_SERIAL--B-->> %p\n", HDD_SERIAL );

	return 0;
}


------------------------- end of main.cpp -------------------------


----------------------- LinuxHardwareInfo.cpp -----------------------

#include <stdio.h>
#include "LinuxHardwareInfo.h"

void createStorageFiles(void)
{


printf("\n\n\n\n-createStorageFiles- HDD_SERIAL-00->> %s",  HDD_SERIAL);

printf("\n\n\n\ncreateStorageFiles ADDRESS OF H HDD_SERIAL-0->> %p\n",  HDD_SERIAL);


printf("\n\n\n\ncreateStorageFiles- HDD_FIRMWARE-11->> %s",  HDD_FIRMWARE);

printf("\n\n\n\ncreateStorageFiles- ADDRESS OF HDD_FIRMWARE -1->> %p\n",  HDD_FIRMWARE);


printf("\n\n\n\ncreateStorageFiles- HDD_SERIAL_C-22->> %s",  HDD_SERIAL_C);

printf("\n\n\n\n----createStorageFiles--- ADDRESS OF HDD_SERIAL_C------2-->> %p\n",  HDD_SERIAL_C);


}

-------------------- end of LinuxHardwareInfo.cpp -------------------

----------------------- LinuxHardwareInfo.h -----------------------

#ifndef LINUXHARDWAREINFO_H
#define LINUXHARDWAREINFO_H


#define HDD_SERIAL_C "zzz yyy"
#define HDD_SERIAL "Serial Number"
#define HDD_FIRMWARE "nhhhj"
void createStorageFiles(void);

#endif

-------------------- end of LinuxHardwareInfo.h -------------------


-------------------- outputs -------------------

------------AA-------------HDD_SERIAL-----------AA----------------->> 



------A--------- ADDRESS OF HDD_SERIAL-------A----------->> 0x80487c0




---------createStorageFiles--------- HDD_SERIAL-----------00---------------->> Serial Number



----createStorageFiles--- ADDRESS OF H HDD_SERIAL------0---------->> 0x80488ec




---------createStorageFiles--------- HDD_FIRMWARE -----------11---------------->> nhhhj



----createStorageFiles--- ADDRESS OF HDD_FIRMWARE -----1---------->> 0x80489a1




---------createStorageFiles--------- HDD_SERIAL_C-----------22---------------->> zzz yyy



----createStorageFiles--- ADDRESS OF HDD_SERIAL_C------2---------->> 0x8048a51




------------BB-------------HDD_SERIAL-----------BB----------------->> 



------B--------- ADDRESS OF HDD_SERIAL-------B----------->> 0x80487c0\
 
Old 03-23-2010, 12:49 AM   #2
orrt
LQ Newbie
 
Registered: Mar 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Its ok its working now. I included the "LinuxHardwareInfo.h" file last and it started working. I don't know why but if someone else does I'd really like to know.
 
Old 03-23-2010, 02:17 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
orrt ... not sure why you are having either issue??

I used your code without createStorageFiles(); in the main and remarked out in the header and all worked
swimmingly.
I am also able to put the headers in any order I choose.
 
Old 03-23-2010, 06:14 PM   #4
orrt
LQ Newbie
 
Registered: Mar 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by grail View Post
orrt ... not sure why you are having either issue??

I used your code without createStorageFiles(); in the main and remarked out in the header and all worked
swimmingly.
I am also able to put the headers in any order I choose.


Thanks for the reply and it works on Windows . I'm running Ubuntu 8.10 and the probelm is back. I really don't know what is wrong. I'm using the GNU C++ compiler and this is my command line:

g++ main.cpp LinuxHardewareInfo.cpp LinuxHardwareInfo.h -o sysinfo


I'm really stumped on this one.
 
Old 03-23-2010, 07:54 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I did the same on ubuntu 9.10 and as i said it worked fine. Are you getting any error from the compile command you submitted?

Maybe try just: g++ main.cpp LinuxHardewareInfo.cpp

This will produce the old favourite a.out. See how that goes?
 
Old 03-23-2010, 08:16 PM   #6
orrt
LQ Newbie
 
Registered: Mar 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by grail View Post
I did the same on ubuntu 9.10 and as i said it worked fine. Are you getting any error from the compile command you submitted?

Maybe try just: g++ main.cpp LinuxHardewareInfo.cpp

This will produce the old favourite a.out. See how that goes?
Thanks but yes, I tried that but to no avail. I also tried:

g++ LinuxHardwareinfo.h -c

followed by:

g++ main.cpp LinuxHardwareInfo.cpp LinuxHardwreInfo.h.gch -o sysinfo

but it still didn't work.

Interestingly enough it works when I change all references that have anything to do with the HDD_SERIAL define and function to "HDD_SERIALZZ" and "getHDDSerialZZ(...)". I think that there must have been a name clash or something with "HDD_SERIAL".

I would like to investigate this further after the deadline for this particular project has passed. But if there was a nameclash wouldn't the compiler have picked it up?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Where is the value that defines SCREEN stored? john test Linux - Newbie 2 09-22-2008 01:24 PM
MAC OS X defines son_t Programming 2 07-24-2007 04:20 AM
typedef defines which type? kpachopoulos Programming 1 11-10-2005 02:46 AM
override defines alaios Programming 1 09-14-2005 10:40 AM
OS Conditional Defines kevin.cowley Programming 1 09-24-2003 03:54 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:16 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration