Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 16th, 2005, 4:03 PM   #1
c0ldshadow
Unverified User
 
c0ldshadow's Avatar
 
Join Date: Jun 2005
Location: NJ
Posts: 23
Rep Power: 0 c0ldshadow is on a distinguished road
Red face Program compiling even without reference to required static link files

hey folks,i havent been doing that much programming over the last few months but ive gotten back into working on some stuff recently. basically the issue i'm having is that im getting a program to compile even though it shouldnt (at least i think).

i have a libgenericwipe project that contains a .cpp,.h,and .dev file in it. this libgenericwipe has not even been compiled to generate any static link libraries.

in a separate folder i have another project for the executable im making.. this project includes the .h file for libgenericwipe. in the linker section for this project, i am linking to nothing.

the program compiles without any errors or linker errors and i am able to call the genericwipe function even without linking to anything..

my guess is im missing something very obvious or im just being stupid.

here are the contents of my project files..

killer.cpp
#include <windows.h>
#include "c:\\c0ldshadow\\libgenericwipe\\genericwipe.h"
int main()
{
	genericWipe("asd");
}

genericwipe.h
#ifndef GENERICWIPE_H
#define GENERICWIPE_H
#include "genericwipe.cpp"
bool genericWipe(const char *);
#endif

genericwipe.cpp
#include <windows.h>
#include <stdio.h>
#include <string.h>
bool genericWipe(const char *what)
{
    //note i removed the code here since its not important for this issue
    return true;
}

maybe its just me but i swear that this program shouldnt compile.. i thought that the genericwipe project would need to be compiled into a static link library and then the killer.cpp would need to add this static link library file to the linker... i have done none of this, yet the code compiles and works fine...

like i said before, i havent been coding in a while so maybe im just doing something blatantly stupid or whatever..

please note that this is issue no emergency but any advice would be great..
regards,--c0ldshadow
__________________
DeepTide

The way is shut.
It was made by those who are dead
and the Dead keep it.
The way is shut.
c0ldshadow is offline   Reply With Quote
Old Sep 16th, 2005, 4:05 PM   #2
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 5 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
I have no professional advice that i've gotten from books or anything, but my guess is that when a compiler sees a file in quotes on an include line it doesn't care what the extension is and just includes it as a header..
__________________

tempest is offline   Reply With Quote
Old Sep 16th, 2005, 4:23 PM   #3
stevengs
Professional Programmer
 
stevengs's Avatar
 
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4 stevengs is on a distinguished road
I would take a closer look at the following line in the genericWipe.h header file:
#include "genericwipe.cpp"
I would consider that at least a bit unorthodox.

Surely you see what is happening?

btw, good to see you around again mon ami.

EDIT: (ok, maybe I need to be more direct: try including "the other way around": include genericWipe.h in genericWipe.cpp.. get it now? )
__________________
-Steven
"Is this a piece of your brain?" - Basil Fawlty
stevengs is offline   Reply With Quote
Old Sep 16th, 2005, 4:23 PM   #4
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Can I suggest removing the path from your local #include?
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Sep 16th, 2005, 4:32 PM   #5
stevengs
Professional Programmer
 
stevengs's Avatar
 
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4 stevengs is on a distinguished road
Quote:
Originally Posted by Ooble
Can I suggest removing the path from your local #include?
Concrete paths, while less elegant and a true pain in the arse when the file structure changes, should not cause problems as long as they are correct. An abstract path would, however, definitely be preferrable.

(In German, we speak of concrete and abstract paths, what are the English counterparts? It doesn't sound quite right for some reason...)
__________________
-Steven
"Is this a piece of your brain?" - Basil Fawlty
stevengs is offline   Reply With Quote
Old Sep 16th, 2005, 4:44 PM   #6
Scorpions4ever
Programmer
 
Join Date: Jun 2005
Posts: 86
Rep Power: 4 Scorpions4ever is on a distinguished road
This is where the problem is:
#include "genericwipe.cpp"
RULE #1: NEVER EVER #include C source. Here be hippogriffs and all that jazz. For one thing, it defeats the purpose of separate compilation, does useless compilation operations and It just invites too much grief (not to mention, duplicate symbol errors at link time with larger projects!).

Remove that line and instead, add genericwipe.cpp to your Makefile. Your new genericwipe.h should look like this:
#ifndef GENERICWIPE_H
#define GENERICWIPE_H

bool genericWipe(const char *);
#endif
The only things that should be in C header files are macros, defines and other preprocessor stuff, function prototypes, structs and typedefs and extern definitions. C++ headers may contain templated code, but that's it.
Scorpions4ever is offline   Reply With Quote
Old Sep 16th, 2005, 4:51 PM   #7
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5 grumpy is on a distinguished road
Quote:
Originally Posted by stevengs
(In German, we speak of concrete and abstract paths, what are the English counterparts? It doesn't sound quite right for some reason...)
If I understood your meaning, we speak of absolute and relative paths
grumpy is offline   Reply With Quote
Old Sep 16th, 2005, 4:55 PM   #8
stevengs
Professional Programmer
 
stevengs's Avatar
 
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4 stevengs is on a distinguished road
Quote:
Originally Posted by Scorpions4ever
...The only things that should be in C header files are macros, defines and other preprocessor stuff, function prototypes, structs and typedefs and extern definitions. C++ headers may contain templated code, but that's it.
Yeah, that was what I was hinting at. coldShadow has programmed before, so I was kinda surprised. I tried to hint indirectly at first, but I guess breaking it over his head is cool too . And reinforcing this point is never a bad thing. (and the #ifndef's to deter include loops are also all too often forgotten when a program is in it's tiny and insignificant beginnings... but then, when (not if!) the project grows, the beast rears it's terrible head and reveals it's horrid visage.

Thank you Grumpy, this particular gnawing at the back of my mind has ceased.
And, I must correct myself.. in German the 'pfad' it is also 'absolut' oder 'relativ'. In my defense, it is after midnight and it has been a difficult week
__________________
-Steven
"Is this a piece of your brain?" - Basil Fawlty

Last edited by stevengs; Sep 16th, 2005 at 5:16 PM.
stevengs is offline   Reply With Quote
Old Sep 16th, 2005, 5:02 PM   #9
c0ldshadow
Unverified User
 
c0ldshadow's Avatar
 
Join Date: Jun 2005
Location: NJ
Posts: 23
Rep Power: 0 c0ldshadow is on a distinguished road
wow. embarrassing mistake by me... i cannot believe i missed that lol. thanks for the advice. i havent been coding in a while and it shows.

peace,-c0ld
__________________
DeepTide

The way is shut.
It was made by those who are dead
and the Dead keep it.
The way is shut.
c0ldshadow is offline   Reply With Quote
Old Sep 16th, 2005, 5:11 PM   #10
Scorpions4ever
Programmer
 
Join Date: Jun 2005
Posts: 86
Rep Power: 4 Scorpions4ever is on a distinguished road
By the way, the ifndef's won't save you linking errors either. For instance:
genericwipe.h
============
#ifndef GENERIC_WIPE_H
#define GENERIC_WIPE_H

void genericwipe(char *);
#include "genericwipe.cpp"
#endif

main.cpp
========
#include "genericwipe.h"
int main(void) {
   ...
}


someotherfile.cpp
===============
#include "genericwipe.h"
int somefunc(void) {
   ...
}
Now, both cpp files include genericwipe.h, which in turn includes genericwipe.cpp. On compilation, both will compile fine, but when it comes time to link them all into a final executable, the linker will complain about duplicate symbol errors. Why? Because genericwipe.h included genericwipe.cpp and therefore, copies of all the code of genericwipe.cpp are now in main.obj as well as someotherfile.obj.

This is why you should never put source code (or variable declarations) in header files .
Scorpions4ever is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:34 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC