Jump to content
  • Advertisement
Sign in to follow this  
ISDCaptain01

how to deal with multiple source files

This topic is 1877 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Im having a bit of trouble with multiple source files

 

Say I declare a function prototype in my header file

 

header.h

#pragma once
#include <iostream>

int foo;

void Function(int);

source.cpp

#include "header.h"


void function()
{
   foo = 5;
}

main.cpp

#include "header.h"


int main()
{

   function();
   return 0;
}

for some reason visual c++ gives me trouble when I do this and my code refuses to compile. Why is this?

Share this post


Link to post
Share on other sites
Advertisement

Your header file declares a function called "Function" that takes an int.

 

You main.cpp and source.cpp use a function called "function" (note the difference in case) that doesn't take any parameters.

 

Also, your global int "foo" should be declared (i.e. "int foo;") in one of your .cpp files and externed in header.h (i.e. "extern int foo;").

Share this post


Link to post
Share on other sites
You have two functions, both with differing case and differing parameters:

void Function(int);
void function();

You make an attempt to call "function" in main.cpp, but header.h doesn't expose the proper prototype for "void function"; instead, header.h provides a prototype (without a definition) for "void Function(int)". You will also need to move "int foo;" to source.cpp, and expose a prototype for it "extern int foo;" in header.h.


See Organizing Code Files in C and C++ for more information.

Share this post


Link to post
Share on other sites

It might help if you posted the error message. Though at a guess you're hitting problem four listed in the article fastcall22 linked.

Share this post


Link to post
Share on other sites

You declare "int foo" at global scope two times because separate compilation units call the same header.  Move "int foo" to the CPP file that uses it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!