Skip to main content
GameDev.net gamedev.net
🔒 Locked

[C++] Linking Source Files

Started by lcressey Apr 23, 2011 at 10:19 AM 5 replies 2k views
Original Post
lcressey
lcressey
Hello,

I'm attempting at building a Win32 Console Project using Visual C++ which is to serve as a knowledge hub for a game I play.

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

int main()
{
using namespace std;

// Opening Statements
cout << "Welcome to Luther's Combine Database" << endl;
cout << "Current Version: v1.0" << endl;
cout << "Which category are you interested in?" << endl;

//Categories
int mainchoice;

cout << "Ships" << endl;
cout << "Vehicles" << endl;

cin >> mainchoice;


return 0;
}


There is the basic code for the first screen. The only problem I have, is not knowing how to link two source files together, so that if the user types in "Ships", the program jumps to "ships.cpp", and the program continues.


if (mainchoice = ships)
program runs "ships.cpp"

if (mainchoice = vehicles)
program runs "vehicles.cpp"



For a program like this, i'm looking to implement lots of these little links, but I'm unsure of the command to do so.

Any help would be appreciated,

Regards,
Luke
asdfg__12
asdfg__12
Hi, you cannot "run" a source file or "jumo into" a source file. You can, however, call a function defined within another source file. From the pseudo code that you posted, it seems that you want to have a "ships.cpp" file and within that file you want to define a function named "ships", and just call that function from "main()". It's probably time to learn about functions, if you haven't already.
lcressey
lcressey
Apologies, I have learned basic C++ but its been a while since I did anything with it.

Thanks for the assistance and i'll post the results later (:
rip-off
rip-off
C++ is a statically compiled language. The source files are completely transformed into an executable before it is run. The compiler is not available once you are running the program, so you cannot "run" source files. In contrast, in some dynamic languages it would be possible to do this.

As D_Tr mentioned, what you want to do is call one or more functions that can be defined in separate files. You must instruct your compiler to compile and link all the files into one executable, otherwise you'll get errors. Your IDE will manage this for you automatically.
lcressey
lcressey
In order to call the function fighters(); i have used #include "fighters.cpp"

Snippet from main.cpp
// Main Ships Menu
void ships()
{
using namespace std;
cout << "Luther's Combine Ship Database v1.0" << endl;
cout << "Please make a selection" << endl;

int shipchoice;

cout << "1- Fighters" << endl;
cout << "2- Freighters" << endl;
cout << "3- Capitals" << endl;

cin >> shipchoice;
switch (shipchoice)
{
case 1:
fighters();
break;


Snippet from fighters.cpp

void fighters()

{
using namespace std;
cout << "Luther's Ships Database v1.0" << endl;
cout << "Fighters" << endl;
}


When trying to build the program, i get the following error message

1>fighters.obj : error LNK2005: "void __cdecl fighters(void)" (?fighters@@YAXXZ) already defined in Luthers Combine Database v1.0.obj
1>C:\Users\LUKE\Documents\Combine Database\v1.0\Project\Luthers Combine Database v1.0\Debug\Luthers Combine Database v1.0.exe : fatal error LNK1169: one or more multiply defined symbols found


Yet I havn't defined "fighters" in the main code, only called fighters();

How can I fix this?

Regards,
Luke
the_edd
the_edd

In order to call the function fighters(); i have used #include "fighters.cpp"


You need to put the function declaration in a header (fighters.h, probably) and #include that in fighters.cpp and any source file that wants to call your fighters function.

fighters.h might look like this:


#ifndef FIGHTERS_H_INCLUDED
#define FIGHTERS_H_INCLUDED

void fighters();

#endif // FIGHTERS_H_INCLUDED
lcressey
lcressey
Thanks, very much appreciated

Got it working now, so should be set for a while. Its just a case of knuckling down and getting some hard work done

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.