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

[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

  • [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


  • : [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]
  • : [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.

  • : : [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]

  • [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]
  • Thanks guys, problem solved!
  • : [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]

  • 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]
  • : [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?

  • : 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]

  • [b][red]This message was edited by AsmGuru62 at 2003-3-13 7:52:16[/red][/b][hr]
    [blue]What you did - is just duplicated the function body twice in the project. Besides, the code is not supposd to be placed in a header file. I thought, your point was that guards in include files do not work, meaning thet if you include it twice with guards - the compiler will parse it twice, too. That is not the case - guards work, but you have to provide correct coding![/blue]


  • : [b][red]This message was edited by AsmGuru62 at 2003-3-13 7:52:16[/red][/b][hr]
    : [blue]What you did - is just duplicated the function body twice in the project. Besides, the code is not supposd to be placed in a header file. I thought, your point was that guards in include files do not work, meaning thet if you include it twice with guards - the compiler will parse it twice, too. That is not the case - guards work, but you have to provide correct coding![/blue]
    :
    :
    :


    Yes, but what my point was, as I said earlier..

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

    Sorry if it was confusing, I just meant you can't have function definitions in the header files, only the declarations. Inclusion guards do nothing to help that :)



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

  • : Thanks guys, problem solved!
    :

    It seems like I spoke too soon; its stopped working again!!!!!!!

    I've posted a *stripped-down* version of the program here:

    [blue]http://www.matthill.demon.co.uk/VC/OcrWisard.zip[/blue]

    I have declared a "silly function" 'ack()' in the file body.h which is to be called from within the OcrWisardDlg.cpp and OcrWisard.cpp files, but I'm getting the wretched error:

    [blue]OcrWisardDlg.obj : error LNK2005: "void __cdecl ack(void)" ([email protected]@YAXXZ) already defined in OcrWisard.obj
    Debug/OcrWisard.exe : fatal error LNK1169: one or more multiply defined symbols found[/blue]

    Thanks for any insights!

    Matt.

  • : : Thanks guys, problem solved!
    : :
    :
    : It seems like I spoke too soon; its stopped working again!!!!!!!
    :
    : I've posted a *stripped-down* version of the program here:
    :
    : [blue]http://www.matthill.demon.co.uk/VC/OcrWisard.zip[/blue]
    :
    : I have declared a "silly function" 'ack()' in the file body.h which is to be called from within the OcrWisardDlg.cpp and OcrWisard.cpp files, but I'm getting the wretched error:
    :
    : [blue]OcrWisardDlg.obj : error LNK2005: "void __cdecl ack(void)" ([email protected]@YAXXZ) already defined in OcrWisard.obj
    : Debug/OcrWisard.exe : fatal error LNK1169: one or more multiply defined symbols found[/blue]


    As I said earlier, you CAN'T define a function in a header file and include it more than once, or you will always get "multiply defined symbols" errors.

    You can only define functions in a header file if you only include it once in your program.

    To fix the problem, either only include the .h file once, or do it the right way... only include the function declarations in the header files (put the definitions in a C or C++ file) and then you can include the .h file as many times as you want.

    :
    : Thanks for any insights!
    :
    : Matt.
    :
    :

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

Sign In or Register to comment.

Howdy, Stranger!

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

Categories