Use of extern Question.
-
For example, suppose a project is composed of these 2 files: //file1 extern int socket1; //file2 int socket1; Is possible to test for the existence of int socket1 during compilation if file2 is not part of the build, using some kind of compiler directives so that the project will compile/build normally?
-
For example, suppose a project is composed of these 2 files: //file1 extern int socket1; //file2 int socket1; Is possible to test for the existence of int socket1 during compilation if file2 is not part of the build, using some kind of compiler directives so that the project will compile/build normally?
A way to test for the existence of int socket1 in file1 would be use the preprocessor directives:
#ifdef socket1 extern int socket1; #endif
This will tell the preproccesor (what happens before compilation) to only use the code contained between "#ifdef socket1" and "#endif". If socket1 isn't defined (in file2) then the code between the preproccesor directives will not be compiled at all. You will have to be careful though. Assuming you have code that used socket1 in file1 then you will get compile errors saying that socket1 isn't defined. To combat this you can do the following:#ifdef socket1 extern int socket1; #else int socket1; #endif
I believe that answers your question. Greba, My lack of content on my home page should be entertaining. -
For example, suppose a project is composed of these 2 files: //file1 extern int socket1; //file2 int socket1; Is possible to test for the existence of int socket1 during compilation if file2 is not part of the build, using some kind of compiler directives so that the project will compile/build normally?
jerry1211a wrote: Is possible to test for the existence of int socket1 during co Not directly, because the information you're looking for is not available at compile time, only link time. The
extern int socket1
line tells the compiler "there's anint
defined somewhere calledsocket1
" and that's all it needs. It's not until the linker comes along that the build process actually checks to see thatsocket1
is defined somewhere. That's why "unresolved external" errors are linker errors. --Mike-- LINKS~! Ericahist | 1ClickPicGrabber | CP SearchBar v2.0.2 | C++ Forum FAQ | You Are Dumb Magnae clunes mihi placent, nec possum de hac re mentiri.