Simple Question Regarding Functions in Separate Files

This is a discussion on Simple Question Regarding Functions in Separate Files within the C++ Programming forums, part of the General Programming Boards category; I'm borderline embarrassed to be posting a question that I know the answer is probably going to be super-trivial to. ...

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    10

    Simple Question Regarding Functions in Separate Files

    I'm borderline embarrassed to be posting a question that I know the answer is probably going to be super-trivial to.

    All I'm trying to do is call a function from my main file that I've declared/defined in a separate file, but I'm getting a link error when I try to do it and don't know what the problem is.

    hello_world.cpp:
    Code:
    #include "stdafx.h"#include <stdio.h>
    
    
    #include "print.h"
    
    
    int main() {
    
    
        printTest();
    
    
        return 0;
    }
    print.h:
    Code:
    void printTest();
    print.cpp:
    Code:
    #include <stdio.h>
    
    
    void printTest()
    {
        cout << "test!" << endl;
    }
    If someone could put me out of my misery here I'd be most obliged
    Last edited by chopshardiman; 08-20-2012 at 08:58 AM.

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    10
    Should have included the error I'm getting obv:

    Code:
    Error	1	error LNK2001: unresolved external symbol "void __cdecl printTest(void)" (?printTest@@YAXXZ)	hello_world.obj

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Include the print.h file in print.cpp and see what happens

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,701
    It looks like you forgot to add print.cpp to your project. Your error message is telling you it can't find the implementation.

    Jim

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    10
    Quote Originally Posted by jimblumberg View Post
    It looks like you forgot to add print.cpp to your project. Your error message is telling you it can't find the implementation.

    Jim
    Yep, that was it - I had assumed creating a new file and putting it in the directory would do the job, thanks for the help!

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    2,351
    obviously you're using visual studio. why not just use the project system that's built into it? it handles compiling the proper files automatically.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. a simple question on functions
    By everyone0 in forum C Programming
    Replies: 7
    Last Post: 05-31-2010, 06:07 AM
  2. Functions in a separate file
    By Fox101 in forum C Programming
    Replies: 1
    Last Post: 01-29-2008, 01:53 PM
  3. Using strtok on separate functions
    By Thumper333 in forum C Programming
    Replies: 2
    Last Post: 10-24-2004, 02:19 PM
  4. a simple question about files
    By arian in forum C++ Programming
    Replies: 12
    Last Post: 09-24-2004, 10:54 PM
  5. Replies: 7
    Last Post: 10-15-2002, 10:55 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21