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. multiple c files in visual studio 6 project

multiple c files in visual studio 6 project

Scheduled Pinned Locked Moved C / C++ / MFC
csharpvisual-studioquestion
9 Posts 7 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.
  • A Offline
    A Offline
    Ayman Mashal
    wrote on last edited by
    #1

    Hi i want to create a project in visual studio 6 that contains multipe C source files and header files but i cant understand how a file can access a function or a variable declared in the other file ? and i want to know if its right to add #includes for the standard libraries in all files ? thanks

    C R D 3 Replies Last reply
    0
    • A Ayman Mashal

      Hi i want to create a project in visual studio 6 that contains multipe C source files and header files but i cant understand how a file can access a function or a variable declared in the other file ? and i want to know if its right to add #includes for the standard libraries in all files ? thanks

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      Ayman Mashal wrote:

      but i cant understand how a file can access a function or a variable declared in the other file ?

      What you need to do is provide a function prototype of your function that will be declared in your header file. Then the function body will be in the cpp file. In the cpp file in which you would like to use this function, simply include the header file. If the function is defined somewhere in a cpp file, then the linker will do its job ;).

      Ayman Mashal wrote:

      and i want to know if its right to add #includes for the standard libraries in all files ?

      A good practice is to include only the files you need in your file.


      Cédric Moonen Software developer
      Charting control [v1.1]

      1 Reply Last reply
      0
      • A Ayman Mashal

        Hi i want to create a project in visual studio 6 that contains multipe C source files and header files but i cant understand how a file can access a function or a variable declared in the other file ? and i want to know if its right to add #includes for the standard libraries in all files ? thanks

        R Offline
        R Offline
        Rajesh R Subramanian
        wrote on last edited by
        #3

        Ayman Mashal wrote:

        i cant understand how a file can access a function or a variable declared in the other file ?

        For functions, Cedric Moonen has answered you. For variables, look at the keyword extern for doing this.

        Ayman Mashal wrote:

        i want to know if its right to add #includes for the standard libraries in all files ?

        It is perfectly safe to #include the libraries in all the files, until they are guarded with #ifndef, #define and #endif to prevent it from being included multiple times.


        Nobody can give you wiser advice than yourself. - Cicero ப்ரம்மா

        A 1 Reply Last reply
        0
        • R Rajesh R Subramanian

          Ayman Mashal wrote:

          i cant understand how a file can access a function or a variable declared in the other file ?

          For functions, Cedric Moonen has answered you. For variables, look at the keyword extern for doing this.

          Ayman Mashal wrote:

          i want to know if its right to add #includes for the standard libraries in all files ?

          It is perfectly safe to #include the libraries in all the files, until they are guarded with #ifndef, #define and #endif to prevent it from being included multiple times.


          Nobody can give you wiser advice than yourself. - Cicero ப்ரம்மா

          A Offline
          A Offline
          Ayman Mashal
          wrote on last edited by
          #4

          lets say that i have header.h file that contains types,variables,defines declarations and that i have main.h file that include header.h file and main.h is included in many source files . now when i try to build the project i get that the variables in header.h already defined in the obj file . so how can i deal with this ? thanks

          C T 2 Replies Last reply
          0
          • A Ayman Mashal

            lets say that i have header.h file that contains types,variables,defines declarations and that i have main.h file that include header.h file and main.h is included in many source files . now when i try to build the project i get that the variables in header.h already defined in the obj file . so how can i deal with this ? thanks

            C Offline
            C Offline
            Cedric Moonen
            wrote on last edited by
            #5

            You need to surround your header file with include guards. Something similar to this:

            #ifndef MYFILE_H
            #define MYFILE_H

            ... The code of your header comes here

            #endif

            You need to replace MYFILE_H by something unique in your project. In general, you can simply use the filename.


            Cédric Moonen Software developer
            Charting control [v1.1]

            E 1 Reply Last reply
            0
            • A Ayman Mashal

              lets say that i have header.h file that contains types,variables,defines declarations and that i have main.h file that include header.h file and main.h is included in many source files . now when i try to build the project i get that the variables in header.h already defined in the obj file . so how can i deal with this ? thanks

              T Offline
              T Offline
              toxcct
              wrote on last edited by
              #6

              in headers, you shouldn't define variables. you should only declare extern variables, that means to the compiler that the variable has been defined somewhere in a c/cpp file ; it's the linker job to synthetize this.


              [VisualCalc][Flags Beginner's Guide] | [Forums Guidelines][My Best Advice]

              1 Reply Last reply
              0
              • C Cedric Moonen

                You need to surround your header file with include guards. Something similar to this:

                #ifndef MYFILE_H
                #define MYFILE_H

                ... The code of your header comes here

                #endif

                You need to replace MYFILE_H by something unique in your project. In general, you can simply use the filename.


                Cédric Moonen Software developer
                Charting control [v1.1]

                E Offline
                E Offline
                ensger
                wrote on last edited by
                #7

                Why not only #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 at the top of the .h-file? Seems to work fine.

                P 1 Reply Last reply
                0
                • E ensger

                  Why not only #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 at the top of the .h-file? Seems to work fine.

                  P Offline
                  P Offline
                  prasad_som
                  wrote on last edited by
                  #8

                  ensger wrote:

                  at the top of the .h-file? Seems to work fine.

                  Yes, it will. Way suggested by Cedric is one of the ways. And using pragma's make code dependent on particular compiler.

                  Prasad Notifier using ATL | Operator new[],delete[][^]

                  1 Reply Last reply
                  0
                  • A Ayman Mashal

                    Hi i want to create a project in visual studio 6 that contains multipe C source files and header files but i cant understand how a file can access a function or a variable declared in the other file ? and i want to know if its right to add #includes for the standard libraries in all files ? thanks

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #9

                    Ayman Mashal wrote:

                    and i want to know if its right to add #includes for the standard libraries in all files ?

                    Technically you can, but it's bad practice. Put them all in a single file common to all of your project. Since those files are not going to be changing, take advantage of precompiled headers by turning on precompiled headers for that single file.


                    "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

                    "Judge not by the eye but by the heart." - Native American Proverb

                    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