undefined reference to function code blocks

半城伤御伤魂 提交于 2019-12-11 16:09:29

问题


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


回答1:


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!



回答2:


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 ?




回答3:


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!




回答4:


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




回答5:


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



来源:https://stackoverflow.com/questions/18841735/undefined-reference-to-function-code-blocks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!