Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Use of extern Question.

Use of extern Question.

Scheduled Pinned Locked Moved C / C++ / MFC
questiontutorial
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jerry1211a
    wrote on last edited by
    #1

    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?

    G M 2 Replies Last reply
    0
    • J jerry1211a

      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?

      G Offline
      G Offline
      greba
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • J jerry1211a

        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?

        M Offline
        M Offline
        Michael Dunn
        wrote on last edited by
        #3

        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 an int defined somewhere called socket1" and that's all it needs. It's not until the linker comes along that the build process actually checks to see that socket1 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.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups