Thread: Help with multiple cpp files

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    14

    Help with multiple cpp files

    I'm having problems getting any programs with multiple cpp files to compile. I am using Dev-C++.
    Here are my files:

    Code:
    //main.cpp
    #include <iostream>
    #include "functions.h"
    
    using namespace std;
    
    int main()
    {
    newFunction();
    return 0;
    }
    Code:
    //functions.h
    void newFunction();
    Code:
    //functions.cpp
    #include <iostream>
    #include "functions.h"
    
    void newFunction()
    {
    cout << "Hello \n";
    }
    Here is the error:
    Code:
    C:\Dev-Cpp\functions.cpp `cout' undeclared (first use this function)

    I have tried to get several of these multi-cpp programs to compile but they all give various errors. Even a very simple one like this. I think I must be missing something that should be obvious here.

    Thanks!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This has less to do with having multiple source files and more to do with incorrect code in a source file. In particular, you are using std::cout without fully qualifying the name. As such, a using declaration or using directive should be present, but none are present. The using directive that you do have is present in another source file.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You will need to prefix cout with std::, or add using namespace std, or some similar.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    14
    Thank you both for the quick reply. Still getting errors though strangely. I've updated functions.cpp so now it looks like this

    Code:
    #include <iostream>
    #include "functions.h"
    
    using namespace std;
    
    void newFunction()
    {
    cout << "Hello \n";
    }
    [Linker error] undefined reference to `std::string::size() const'
    [Linker error] undefined reference to `std::string:perator[](unsigned int) const'
    [Linker error] undefined reference to `std::ios_base::Init::Init()'
    C:\Dev-Cpp\Makefile.win [Build Error] [GAYPROJECT.exe] Error 1


    Any ideas?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Sounds like Dev-C++ is invoking gcc instead of g++, so the standard library is not linked to by default. Unfortunately, I do not have a copy of Dev-C++ around to check what are the required changes to make in the settings.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    14

    Cool

    Quote Originally Posted by laserlight View Post
    Sounds like Dev-C++ is invoking gcc instead of g++, so the standard library is not linked to by default. Unfortunately, I do not have a copy of Dev-C++ around to check what are the required changes to make in the settings.
    ok thanks, i thought it would be something that had to do with the linker.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Sclorch
    ok thanks, i thought it would be something that had to do with the linker.
    In a way, it does, because this is a linking problem. Did you create a C project instead of a C++ project?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    14
    Quote Originally Posted by laserlight View Post
    In a way, it does, because this is a linking problem. Did you create a C project instead of a C++ project?
    Yep, am I wrong in assuming that the compiler will just link all of my files in a project together for me? Or do I have to manually link them all together?

    Here were the steps I took:

    Created new project (win32 console)
    Added blank files into project.
    Renamed those blank files to the source and header files.
    Added the code
    Then just clicked "compile and run"

    Am I missing a step here? Thanks again

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Sclorch
    Yep, am I wrong in assuming that the compiler will just link all of my files in a project together for me? Or do I have to manually link them all together?
    It should, but the problem here is that it is not linking to the C++ standard library.

    Quote Originally Posted by Sclorch
    Am I missing a step here?
    They look fine to me. I suggest that you start a new project and test with:
    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
        using namespace std;
    
        string text = "hello world!";
        cout << text << endl;
    }
    If you do not get any linker errors, you might to try adding more files to that working project.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Jan 2009
    Posts
    14
    Quote Originally Posted by laserlight View Post
    It should, but the problem here is that it is not linking to the C++ standard library.


    They look fine to me. I suggest that you start a new project and test with:
    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
        using namespace std;
    
        string text = "hello world!";
        cout << text << endl;
    }
    If you do not get any linker errors, you might to try adding more files to that working project.
    Cool I wrote a small program that uses 2 cpp and 1 new h file that I wrote. Working great so far! thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 02-12-2009, 02:39 PM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. Windows shell commands - multiple files
    By Magos in forum Tech Board
    Replies: 3
    Last Post: 02-28-2006, 01:56 AM
  4. copy multiple files to a destination file
    By Bones in forum C++ Programming
    Replies: 2
    Last Post: 10-02-2003, 10:47 AM
  5. opening multiple files sequentially
    By moonwalker in forum C Programming
    Replies: 5
    Last Post: 08-20-2002, 09:57 PM