Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
I apologize for my gnorance. I didn't know it. But this would lead to severe code size increase. I instead want to have only one function implementation for each use (in this case 3 implementations in machine language) while putting the template in the header would lead to potentially 3*n where is the number of inclusions
I didn't put it in the header, I compiled the code and the compiler accepts it. I still have to run the function to see if it works properly
thanks I knew it but I still had compiler errors so I changed it
I will check it better but doing this it didn't work:
Code:File1.h: extern int function(); File1.cpp: #include "File1.h" int function2() { .. function(); } int function() { .. } File2.cpp #include "File1.h" int function3() { function(); }
Ehm, I don't think you understood. You make an extern declaration in your header-file (x.h), then define the function ONCE in a x.cpp, including the SAME x.h just to make sure that the functions match up.
So, for example:
This will give you ONE funcx, which is called from wherever it is needed.Code:// x.h: extern int funcx(void); // x.cpp #include "x.h" int funcx(void) { return 7; }
In your case, the template function is only needed inside the recipe.cpp, right? Because the functions using the templated functions are all in recipe.cpp.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
No excuse me now the thread has separated in two different discussions. the original discussion is about a template used by different modules. In that case I put the implemenation inside Recipes.cpp but i use it also elsewhere. This discussion is up to point #17
The second discussion from #18 is only about elementary C syntax. I had to use the macro EXTERN (with or without templates in several examples) cause otherwise I had compile time errors. If you like I can do it again and tell you the errors I got. The alternative structures that worked are:
(1)
(2)Code:File1.h extern int func(); File1.cpp int func(); .. .. func(); .. int func() { .. } File2.cpp #include "File1.h" ... func(); ...
I prefer (2) cause I have only one declaration for each functionCode:File1.h #ifndef FILE1_CPP_ #define EXTERN extern #else #define EXTERN #endif EXTERN int func(); File1.cpp #define FILE1_CPP_ #include "File1.h" .. .. func(); .. int func() { .. } File2.cpp #define FILE2_CPP_ #include "File1.h" ... func(); ...
While the following did not work:
(3)
Code:File1.h extern int func(); File1.cpp #include "File1.h" .. .. func(); .. int func() { .. } File2.cpp #include "File1.h" ... func(); ...
Last edited by mynickmynick; 08-21-2008 at 05:51 AM.
There is no reason to ever do (2) and there is nothing wrong with (3). Post your errors and we'll explain them.
gg
Just to confirm codeplugs statement:
This compiles fine withCode:#include <iostream> extern int func(); int func(); int main() { std::cout << func() << std::endl; } int func() { return 7; }And before you say "but it's not using any include" I'll ensure you that #include makes absolutely no difference to the compiler itself - all the preprocessor does it merge all the included content into one large file - I did that manually by putting the extern and prototype declarations in the file [because I already have 100s of little test-files in my temp-work directory, and it's hard enough to track what contains somewhat useful code and what doesn't without having to figure out which .h file belongs to which source].Code:g++ -Wall -ansi -pedantic -Wextra a.cpp
One possible scenario is of course that some type that you declare is changing between one source and another [perhaps due to different include files or different order of includes].
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
i made a test and you are definitely right
i substituted some of the EXTERN with extern in a couple of header files and the compilation was still succesful
But believe me i had errors in an older version of this program I am working on and i solved it in that nasty way
Probably there was something else wrong which had this side effect
I will start desabling some (or probably all of
withCode:#ifndef xx_CPP_ #define EXTERN extern #else #define EXTERN #endif
and let you know if some errors arise (due to something else wrong)Code:#define EXTERN extern
I got it.
The problem is not with functions. the problem is with variables. (Sorry I forgot that!)
So this (nasty) works (with some warnings "EXTERN redefined")
(1)
if i substitute EXTERN with extern in front of CameraArgs I have this error:Code:Camera.h: #ifndef CAMERA_CPP_ #define EXTERN extern #else #define EXTERN #endif .. EXTERN Type_CameraArgs CameraArgs[MAX_STATIONS_NUMBER][MAX_NUMBER_OF_CAMERAS_PER_STATION]; .. EXTERN void *CameraThreadFunction(void *pContext); Camera.cpp #include "Camera.h" void *CameraThreadFunction(void *pContext) { .. } Station.cpp #include "Camera.h" .. CameraArgs.. CameraThreadFunction(); ..
Station.cpp : undefined reference to CameraArgs
The reason for this nasty code is that I hate to declare things more than once
Do you think it has important drawbacks (except for the "EXTERN redefined" warning)?
Yes, in the case of variables, that's true - you have to declare the variable somewhere - usually in ONE .cpp file.
You can use the #define trick [actually, extern on functions make no difference - so you could just REMOVE all the EXTERN on funcitons].
However, it's not a particularly great idea to have a global variable - perhaps you should have an interface to get the cameraargs out of the camera.cpp file instead.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
I would just forget about the #define trick - it just complicates and confuses.
For function declarations, no need for "extern" - add em if you like.
For global variables in header files, *always* use "extern" - then define the global in only one source file.
gg