Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Adding C++ sourcecode files to a VC++ project

r4471r4471 Member Posts: 13
[b][red]This message was edited by r4471 at 2003-3-10 16:37:31[/red][/b][hr]
Hi

I'm trying to add my own sourcecode files (C++) to my VC++ project. However, I get an error "cannot compile precompilation header directive". I have added the files to the project in the fileview window. Is what I am atempting legitimate? Finally, how do I call the functions in the sourcecode from within the visual environment?

Thanks,

Matt.


«1

Comments

  • xhimxhim Member Posts: 108
    [red]
    hi,
    I think this should work fine..add your header file into the file you want to use functions in..or alternatively add that file in stdafx.h...so you dont have have to add it evrywhere you want those functions..and call the class and make an object of that..and use it..likewise..I hope I am clear..and also it works..
    Cheers
    Himanshu


  • AsmGuru62AsmGuru62 Member Posts: 6,519
    : [b][red]This message was edited by r4471 at 2003-3-10 16:37:31[/red][/b][hr]
    : Hi
    :
    : I'm trying to add my own sourcecode files (C++) to my VC++ project. However, I get an error "cannot compile precompilation header directive". I have added the files to the project in the fileview window. Is what I am atempting legitimate? Finally, how do I call the functions in the sourcecode from within the visual environment?
    :
    : Thanks,
    :
    : Matt.
    :
    :
    :
    [blue]Project -> Settings -> C/C++ tab -> Category: 'Precompiled Headers' -> 'Automatic use of precompiled headers' must be checked and in 'Through header' box must be 'stdafx.h'. Click OK. Select Build -> Rebuild All.[/blue]
  • r4471r4471 Member Posts: 13
    : [blue]Project -> Settings -> C/C++ tab -> Category: 'Precompiled Headers' -> 'Automatic use of precompiled headers' must be checked and in 'Through header' box must be 'stdafx.h'. Click OK. Select Build -> Rebuild All.[/blue]


    Thanks, that seems to have solved the problem. However, where do I add the '#include' statements without getting an error?

    The functions need to be accessed from two of the project files, and if I had the '#include' to both it reports "multiple definition of function ........".

    Adding it to stdafx results in the same thing.

    Thanks,

    Matt.

  • DB1DB1 Member Posts: 1,142
    : : [blue]Project -> Settings -> C/C++ tab -> Category: 'Precompiled Headers' -> 'Automatic use of precompiled headers' must be checked and in 'Through header' box must be 'stdafx.h'. Click OK. Select Build -> Rebuild All.[/blue]
    :
    :
    : Thanks, that seems to have solved the problem. However, where do I add the '#include' statements without getting an error?
    :
    : The functions need to be accessed from two of the project files, and if I had the '#include' to both it reports "multiple definition of function ........".
    :
    : Adding it to stdafx results in the same thing.
    :
    : Thanks,
    :
    : Matt.
    :
    :

    Only #include "something.h" in your others where you need them. If you #include a ".c" or ".cpp" file, or if your included header file has function definitions in it you will get this error.

    [italic][blue]Programming is like kids... One mistake and you have to support it for life[/blue][/italic]

  • AsmGuru62AsmGuru62 Member Posts: 6,519
    [blue]Every include file should be surrounded with the guard:[/blue][code]
    // YourIncludeFileName.h
    #ifndef YourIncludeFileName_h
    #define YourIncludeFileName_h

    // ... your code from H file here ...

    #endif // YourIncludeFileName_h
    [/code][blue]Or, you can use [b]#pragma once[/b], but that is Microsoft specific. If you want to be portable to *IX - use [b]#ifndef[/b]. This way, no need to worry about where you include files - include them a zillion times...[/blue]
  • r4471r4471 Member Posts: 13
    Thanks guys, problem solved!
  • DB1DB1 Member Posts: 1,142
    : [blue]Every include file should be surrounded with the guard:[/blue][code]
    : // YourIncludeFileName.h
    : #ifndef YourIncludeFileName_h
    : #define YourIncludeFileName_h
    :
    : // ... your code from H file here ...
    :
    : #endif // YourIncludeFileName_h
    : [/code][blue]Or, you can use [b]#pragma once[/b], but that is Microsoft specific. If you want to be portable to *IX - use [b]#ifndef[/b]. This way, no need to worry about where you include files - include them a zillion times...[/blue]
    :


    Inclusion guards or not, you will still get multiple definition errors if you have anything more than the function prototypes in the header files, and include them more than once.


    [italic][blue]Programming is like kids... One mistake and you have to support it for life[/blue][/italic]

  • AsmGuru62AsmGuru62 Member Posts: 6,519
    Inclusion guards or not, you will still get multiple definition errors if you have anything more than the function prototypes in the header files, and include them more than once.
    :
    [blue]Did you try to prove that statement with the code and if you have that code - can I see it?[/blue]
  • r4471r4471 Member Posts: 13
    : [blue]Did you try to prove that statement with the code and if you have that code - can I see it?[/blue]

    I "knocked up" a 'small' program to test the the theory - in the header file I put a small function that initialised a variable (just as a test) - but no function prototype! I'm thinking that I could perhaps put the function prototype in a '.cpp' file (same filename as the .h file)?

    How do I workaround this?

  • DB1DB1 Member Posts: 1,142
    : Inclusion guards or not, you will still get multiple definition errors if you have anything more than the function prototypes in the header files, and include them more than once.
    : :
    : [blue]Did you try to prove that statement with the code and if you have that code - can I see it?[/blue]
    :

    Absolutely. Make a new Win32 console app project, and add 3 files to the project, "main.c", "scrap.c", and "scrap.h". In "main.c", add this:
    [code]
    #include
    #include "scrap.h"

    int main()
    {
    printf("answer is %d
    ", GetAnswer());

    PrintAnswer();

    return 0;
    }[/code]
    Now in "scrap.c" add this:
    [code]
    #include
    #include "scrap.h"

    void PrintAnswer(void)
    {
    printf("answer is %d
    ", GetAnswer());

    return;
    }[/code]
    Last file, "scrap.h", add this:
    [code]
    #ifndef SCRAP_H_
    #define SCRAP_H_

    void PrintAnswer(void);
    int GetAnswer(void);

    int GetAnswer(void)
    {
    return 100;
    }

    #endif // SCRAP_H_
    [/code]
    Notice that you will get these errors :
    main.c
    scrap.c
    Linking...
    scrap.obj : error LNK2005: _GetAnswer already defined in main.obj
    Debug/Scrap.exe : fatal error LNK1169: one or more multiply defined symbols found
    Error executing link.exe.

    Scrap.exe - 2 error(s), 0 warning(s)


    Now, try it again, removing the line #include "scrap.h" from the file "scrap.c"... no more errors.


    [italic][blue]Programming is like kids... One mistake and you have to support it for life[/blue][/italic]

«1
Sign In or Register to comment.