C++ MFC

Moderators: Lundin
Number of threads: 3337
Number of posts: 9005

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Adding C++ sourcecode files to a VC++ project Posted by r4471 on 10 Mar 2003 at 4:32 PM
This message was edited by r4471 at 2003-3-10 16:37:31

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.


Report
Re: Adding C++ sourcecode files to a VC++ project Posted by xhim on 10 Mar 2003 at 9:35 PM

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


Report
Re: Adding C++ sourcecode files to a VC++ project Posted by AsmGuru62 on 11 Mar 2003 at 7:13 AM
: This message was edited by r4471 at 2003-3-10 16:37:31

: 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.
:
:
:
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.
Report
Re: Adding C++ sourcecode files to a VC++ project Posted by r4471 on 11 Mar 2003 at 8:43 AM
: 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.


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.

Report
Re: Adding C++ sourcecode files to a VC++ project Posted by DB1 on 11 Mar 2003 at 12:12 PM
: : 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.
:
:
: 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.

Programming is like kids... One mistake and you have to support it for life

Report
Re: Adding C++ sourcecode files to a VC++ project Posted by AsmGuru62 on 11 Mar 2003 at 3:11 PM
Every include file should be surrounded with the guard:
// YourIncludeFileName.h
#ifndef YourIncludeFileName_h
#define YourIncludeFileName_h

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

#endif // YourIncludeFileName_h
Or, you can use #pragma once, but that is Microsoft specific. If you want to be portable to *IX - use #ifndef. This way, no need to worry about where you include files - include them a zillion times...
Report
Thanks - problem solved! Posted by r4471 on 11 Mar 2003 at 3:26 PM
Thanks guys, problem solved!
Report
I spoke too soon! Posted by r4471 on 17 Mar 2003 at 12:43 PM
: 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:

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

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:

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


Thanks for any insights!

Matt.

Report
Re: I spoke too soon! Posted by DB1 on 17 Mar 2003 at 1:55 PM
: : 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:
:
: http://www.matthill.demon.co.uk/VC/OcrWisard.zip
:
: 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:
:
: OcrWisardDlg.obj : error LNK2005: "void __cdecl ack(void)" (?ack@@YAXXZ) already defined in OcrWisard.obj
: Debug/OcrWisard.exe : fatal error LNK1169: one or more multiply defined symbols found



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.
:
:

Programming is like kids... One mistake and you have to support it for life

Report
Function definition problem solved - class definition problem created Posted by r4471 on 18 Mar 2003 at 5:31 AM
: Thanks guys, problem solved!
:

Report
Re: Adding C++ sourcecode files to a VC++ project Posted by DB1 on 11 Mar 2003 at 8:32 PM
: Every include file should be surrounded with the guard:
: // YourIncludeFileName.h
: #ifndef YourIncludeFileName_h
: #define YourIncludeFileName_h
: 
: // ... your code from H file here ...
: 
: #endif // YourIncludeFileName_h
: 
Or, you can use #pragma once, but that is Microsoft specific. If you want to be portable to *IX - use #ifndef. This way, no need to worry about where you include files - include them a zillion times...
:


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.


Programming is like kids... One mistake and you have to support it for life

Report
Re: Adding C++ sourcecode files to a VC++ project Posted by AsmGuru62 on 12 Mar 2003 at 7:34 AM
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.
:
Did you try to prove that statement with the code and if you have that code - can I see it?
Report
Re: Adding C++ sourcecode files to a VC++ project Posted by r4471 on 12 Mar 2003 at 12:14 PM
: Did you try to prove that statement with the code and if you have that code - can I see it?

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?

Report
Re: Adding C++ sourcecode files to a VC++ project Posted by DB1 on 12 Mar 2003 at 2:45 PM
: 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.
: :
: Did you try to prove that statement with the code and if you have that code - can I see it?
:

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:
#include <stdio.h>
#include "scrap.h"

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

	PrintAnswer();

	return 0;
}

Now in "scrap.c" add this:
#include <stdio.h>
#include "scrap.h"

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

	return;
}

Last file, "scrap.h", add this:
#ifndef SCRAP_H_
#define SCRAP_H_

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

int GetAnswer(void)
{
	return 100;
}

#endif // SCRAP_H_

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.


Programming is like kids... One mistake and you have to support it for life

Report
Re: I see, but... Posted by AsmGuru62 on 13 Mar 2003 at 7:51 AM
This message was edited by AsmGuru62 at 2003-3-13 7:52:16

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!


Report
Re: I see, but... Posted by DB1 on 14 Mar 2003 at 1:15 AM
: This message was edited by AsmGuru62 at 2003-3-13 7:52:16

: 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!
:
:
:


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 function prototypes 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 :)



Programming is like kids... One mistake and you have to support it for life

Report
Re: DB1, I completely agree. Posted by AsmGuru62 on 14 Mar 2003 at 6:38 AM




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.