Sign in to follow this  
Scipio3

How to use microsoft Visual C++ 2005 Express edition beta (linking source files)

Recommended Posts

I've been reading a book on C++ and have started trying to do some of the exercises. I've started off writing a simple program that has one function called "addition" that adds two integers. I can get it to work when I put main(), the function prototype, and the function definition in one file. But if I put the definition in one file, the prototype in a header, and main in another .cpp source file, it doesn't work. Is there anything special that must be done here? I made a project in C++ and added the function definition's source file to the project. Is the problem that you need to do something to make the files link together in the visual C++ interface, or do I need do something to the code to get the two source files to link. I'm also able to get it to work if the definition and main are in the same .cpp file and the prototype is in an included header. It seems like I can understand the book, but I'm having trouble using the development interface. -- Scipio3

Share this post


Link to post
Share on other sites
Perhaps if we could see some code? I just ran a test with the function prototype in one header, the definition in one source file, and the main() in another source file and it ran just fine. Here's what I did:

add.h:

int addition(int num1, int num2);



add.cpp:

int addition(int num1, int num2)
{
return num1 + num2;
}



main.cpp:

#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <conio.h>
#include "add.h"

int main()
{
printf("%d", addition(1, 1));
getch();
return 0;
}


Console output: 2

That all compiled and ran fine, so maybe we could see your code and go from there. Hope it helps.

-AJ

Share this post


Link to post
Share on other sites
There are 3 files-- definition1.cpp, header1.h, firstprogram.cpp

I was able to run them when they were in the same file.

Is there anything wrong with the code, or am I using the interface (visual C++) incorrectly. All I've done is made a project, added one header and one .cpp file and just select build from the build menu (build "project").

It might be the case that i introduced some error when I copy/pasted them into the different files.


definition1.cpp:


int Add(int a, int b)
{
int c;
c=a+b;

return c;
}


header1.h:

int Add(int, int);


firstprogram.cpp:

#include "stdafx.h"
#include <iostream>
#include "header1.h"



int _tmain(int argc, _TCHAR* argv[])

{using std::cout;

cout << "60+70 = :" << Add(60,70);




return 0;
}



-- scipio3

Share this post


Link to post
Share on other sites
I couldn't see anything immediately wrong with that. Perhaps you could explain what you mean by "it didn't work"? Was there a compile time error? What was it?

EDIT: Also, it's easier on the eyes to read source with tags. That is, for each snippit of code place it between [ source] CODE HERE [ /source] except with no space between the brackets.

Share this post


Link to post
Share on other sites
Quote:

All I've done is made a project, added one header and one .cpp file and just select build from the build menu (build "project")


The project needs to include all the .cpp files. Not just one.

Share this post


Link to post
Share on other sites
Here's the big ask: What are the errors Visual C++ is throwing up?
If you want a quick and comprehensive reply -- be sure to let us know just precisely what is wrong ;)

~Shiny.

Share this post


Link to post
Share on other sites
A couple of things that come to mind:


1. Put the <iostream> header into the stdafx.h file. You are never going to change that header file and therefore its a great candidate for a precompiled header. Typically, leave the ones you author out. This will increase your compile times significantly.

2. Being as you are using precompiled headers I am going to guess that you are trying to comile either of the cpp files and getting the error - possibly saying it cannot find the procompiled stuff..


If #2 is the case you can do one of two things, either build the SOLUTION (CTRL+SHIFT+B I think...) or R-Click on the Stdafx.cpp, build it first - and then build the other cpp files.

If thats not the case, then as said before - more data.


Hope that helps..

#dth-0

Share this post


Link to post
Share on other sites
This is what it says when I try to build.



------ Build started: Project: First program, Configuration: Debug Win32 ------
Compiling...
definitiion1.cpp
c:\documents and settings\roy sr\my documents\visual studio 2005\projects\first program\first program\definitiion1.cpp(10) : fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?
Build log was saved at "file://c:\Documents and Settings\Roy Sr\My Documents\Visual Studio 2005\Projects\First program\First program\Debug\BuildLog.htm"
First program - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

--Scipio3

Share this post


Link to post
Share on other sites
I got it working.

When I posted the info the program was giving me I was first able to read it.

After I included the stdafx.h file in the other source file (definition.cpp).

--Scipio3

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this