1

I'm doing something wrong, I know. I can't quite figure out how to link two .cpp files together through a header file. The calling method can't see the other source.

I'm using Code::Blocks as an IDE with MinGW.

Any help would be greatly appreciated. It would be even more appreciated if you could show the fixed source, link in the reply to a pastebin page with it.

/***********************************main.cpp***********************************/
#include <iostream>

using namespace std;

#include "test.h"

int main()
{
    printTest();            //can't see printTest, defined in test.cpp
    return 0;
};


/***********************************test.h***********************************/
#ifndef TEST_H_INCLUDED
#define TEST_H_INCLUDED

void printTest();

#endif // TEST_H_INCLUDED


/***********************************test.cpp***********************************/
#include "test.h"

void printTest()
{
    cout << "Hello world!" << endl;
};
4
  • 3
    Your code is correct, how are you calling the compiler? Have you added test.cpp to your project files? The IDE needs to know which files to compile. Commented Feb 22, 2012 at 23:05
  • How are you trying to build your program, and what exactly is your error? The code on the pastebin looks fine. Commented Feb 22, 2012 at 23:06
  • Welcome to StackOverflow. Please edit your question and provide the source here. Questions should be self-contained and not rely on links to external sites; if the external site disappears or is not available, the question becomes meaningless. Also, you should not be requiring people to leave SO in order to get the information to try and help you. Please read the FAQ on how to ask questions here. Thanks. :) Commented Feb 22, 2012 at 23:19
  • 1
    If you don't compile them in a project, you have to #include both the header and implementation file in the main file (CodeBlocks). This is a recollection from my refusal to use pointless projects when it's a short 3-file thing such as this. Add the line #include "test.cpp" below #include "test.h". Tested with C::B and works. Commented Feb 22, 2012 at 23:40

3 Answers 3

4

You might find this code blocks wiki helpful. It looks like Code blocks uses a managed build system so if you add the file to the project properly then it should know to compile it and link in the object file that results.

And just to be more explicit about some other comments, when you use "using namespace std;" the namespace is only brought into scope for the file where the using statement is located. That is why others are telling you to explicitly specify the std:: namespace. You could also bring all of the std namespace into scope in the test.cpp file. Many people consider this a bad habit to get into. It's generally better to bring into scope just what you need via

using std::cout;
using std::endl;

Finally, remember that std::endl adds a new line AND flushes the buffer, it's not a good replacement for a new line character in all cases.

Sign up to request clarification or add additional context in comments.

Comments

1

In test.cpp replace cout << "Hello world!" << endl; by std::cout << "Hello world!" << std::endl;

Comments

0

sanket answer’s seems incomplete to me. You need to add #include <iostream> in your test.cpp so that the compiler knows what "cout" is.

As sanket stated, you should use std::cout and std::endl in test.cpp.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.