0

main.cpp

#include <iostream>
#include <string>
using namespace std;

void echo(string);

int main()
{
    echo("hello");
    cout << "Hello world!" << endl;
    return 0;
}

print.cpp

#include <iostream>
#include <string>
void echo(string code){
   cout << code;
}

After compiling the code in code blocks 12.11, it gives me that error:

undefined reference to `echo(std::string)

I use windows 7 x64. I have added the directory; Project>build options > search directories and added the current working directory. All the files are in one console project in code blocks

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Michael harris
  • 966
  • 2
  • 14
  • 25

5 Answers5

2

I believe you should read up a bit more on namespaces usage. You are missing std in print.cpp.

Generally, while starting to learn cpp or getting a grip of the language you should always try writing full names of the classes along with the namespaces. Eventually with practice and some oversights (like now) you will learn why you really need them. In a nutshell namespaces are great:

  • When you are writing code over multiple files
  • Compartmentalize your code into separate blocks.

Also, using namespace std; should be used within cpp files mostly (otherwise headers get mangled up.

Anyways, try changing your code to this:

#include <iostream>
#include <string>
void echo(std::string code){
    std::cout << code;
}

Then your results will look like this:

 > g++ main.cpp print.cpp -o a.out

 > ./a.out
helloHello world!
0

You should get more than that linker error, since you use string without any namespace in your print.cpp file. And if that source file doesn't compile it can't be linked with, and you will get the linker error you have.

Change to e.g.

void echo(std::string code) { ... }

And you do try to link with the object file created from print.cpp ?

Pranav 웃
  • 8,469
  • 6
  • 38
  • 48
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • I have tried your code and it is Not working; the same and the only error: undefined reference to 'echo(std::string)' – Michael harris Sep 17 '13 at 05:40
  • @Michaelharris Then you simply don't *build* with the `print.cpp` file. – Some programmer dude Sep 17 '13 at 05:41
  • @Michaelharris If the `print.cpp` file isn't built (as indicated by you not having any errors from it even though there is), then the code will not exist in the linking stage and the linker will not find the function, complaining about it being undefined. – Some programmer dude Sep 17 '13 at 05:46
  • So how I build it, It is just a simple c++ multi file code tutorial – Michael harris Sep 17 '13 at 05:47
  • @Michaelharris Try to make a clean rebuild, or change something in `print.cpp` to see in the build-log that it is indeed being built. – Some programmer dude Sep 17 '13 at 05:48
  • I have made an error in print.cpp and found that the compiler do not tell anything about the error – Michael harris Sep 17 '13 at 05:50
  • @Michaelharris Then it seems there is some problem with the project setup in the IDE, and I'm not familiar enough with Code::Blocks to be able to help you further. Once you get `print.cpp` to build properly, then it should work out in the end. – Some programmer dude Sep 17 '13 at 05:51
  • @Michaelharris Most IDEs (I don't know Code::Blocks) you have to tell them which files to compile. C++ isn't Java and IDEs don't normally search through directories for you. So look for the option in Code::Blocks where you can add print.cpp to your project. – john Sep 17 '13 at 06:01
0

I know this is old, but for anyone looking to solve this issue, the following may be a solution for you. If you have g++ follow c++ 11 under project->build options (check your options anyway) then you must check that box for all files you make in the project for the error to be cleared up. I had that annoying undefined reference thing too but now it is gone!

Kaz
  • 23
  • 3
0

Try "Project/Properties/Build Targets tab". There you should find "Build target files" field. In that filed find "print.cpp" and click the checkbox (now the compiler will build print.cpp).

Some usefull information on Project management in CB http://www.codeblocks.org/docs/main_codeblocks_en.html

dmytro.poliarush
  • 383
  • 4
  • 11
0

When dealing with strings in C++ its best to sue std::string and your code seems to be wrong with a changes like using std::cout instead of plain cout another thing you need to be careful is linking your files especially files in different directories you need to tell code blocks were to find this print.cpp by going to build option and go for the search tab directory and point to where print.cpp is other wise the other approach is to just build a project which will have the main.cpp and and then add print.cpp class to current project I hope this will be of some help

Charles
  • 1,844
  • 1
  • 14
  • 21