News:

As usual while waiting for the next release - don't forget to check the nightly builds in the forum.

Main Menu

undefined reference

Started by spartan, November 03, 2017, 04:19:06 PM

Previous topic - Next topic

spartan

I have delete previus topic because I came across few things. Again these are my files and their content

add.h:

#ifndef ADD_H_INCLUDED
#define ADD_H_INCLUDED

int add(int x, int y);

#endif // ADD_H_INCLUDED



add.cpp:

#include <iostream>

int add(int x, int y)
{
    return x+y;
}



main. cpp:

#include <iostream>
#include "add.h"

using namespace std;

int main()
{
    cout << "We add 3 and 5: " << add(3,5);

    return 0;
}

Error is:
in line of main() cout << ... : undefined reference to 'add(int,int)' error: Id returned 1 exit status(I don't know if it's surely Id with I(big i) or l(lowercase L)).

I finally managed to get project manager on screen. My project is called exercise and here is structure of it in project manager:Sources(add.cpp, main.cpp), Headers(add.h), Others(). If anyone has any idea where bug lies pleas help me, I have to start my project as soon as possible.

EDIT: full build log

-------------- Build: Debug in exercise (compiler: GNU GCC Compiler)---------------

g++  -o bin/Debug/exercise obj/Debug/main.o   
obj/Debug/main.o: In function `main':
/home/user/Namizje/CodeblocksProjects/exercise/main.cpp:7: undefined reference to `add(int, int)'
collect2: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 0 second(s))
2 error(s), 0 warning(s) (0 minute(s), 0 second(s))


stahta01

#1
Post a [full] build log or stop wasting time on this website!

Edit2: The below is a hyperlink click on it and follow the directions!!
http://wiki.codeblocks.org/index.php/FAQ-Compiling_(errors)#Q:_How_do_I_troubleshoot_a_compiler_problem.3F

Edit3: Use rebuild instead of build! and post the full rebuild log or I will put you on my list of people not worth trying to help.

Tim S.
C Programmer working to learn more about C++.
On Windows 10 64 bit and Windows 11 64 bit.
--
When in doubt, read the CB WiKi FAQ. [url="http://wiki.codeblocks.org"]http://wiki.codeblocks.org[/url]

stahta01

Still waiting for the full build log!
C Programmer working to learn more about C++.
On Windows 10 64 bit and Windows 11 64 bit.
--
When in doubt, read the CB WiKi FAQ. [url="http://wiki.codeblocks.org"]http://wiki.codeblocks.org[/url]

spartan

#3
Followed your link, everything is already set as it should be.Thanks for help!!

EDIT: here is (I sincerely hope it's full this time) build log

-------------- Clean: Debug in exercise (compiler: GNU GCC Compiler)---------------

Cleaned "exercise - Debug"

-------------- Build: Debug in exercise (compiler: GNU GCC Compiler)---------------

g++ -Wall -fexceptions -g -std=c++14  -c /home/user/Namizje/CodeblocksProjects/exercise/main.cpp -o obj/Debug/main.o
g++  -o bin/Debug/exercise obj/Debug/main.o   
obj/Debug/main.o: In function `main':
/home/user/Namizje/CodeblocksProjects/exercise/main.cpp:7: undefined reference to `add(int, int)'
collect2: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 4 second(s))
2 error(s), 0 warning(s) (0 minute(s), 4 second(s))




stahta01

#4
Add the other source file to the project!

Build -> Rebuild gives me this as the full rebuild log.
-------------- Clean: Debug in exercise (compiler: GNU GCC Compiler)---------------

Cleaned "exercise - Debug"

-------------- Build: Debug in exercise (compiler: GNU GCC Compiler)---------------

i686-w64-mingw32-g++.exe -Wall -fexceptions -g  -c C:\Users\stahta01\devel\open_source_code\no_version_control\Test\exercise\add.cpp -o obj\Debug\add.o
i686-w64-mingw32-g++.exe -Wall -fexceptions -g  -c C:\Users\stahta01\devel\open_source_code\no_version_control\Test\exercise\main.cpp -o obj\Debug\main.o
i686-w64-mingw32-g++.exe  -o bin\Debug\exercise.exe obj\Debug\add.o obj\Debug\main.o   
Output file is bin\Debug\exercise.exe with size 1.69 MB
Process terminated with status 0 (0 minute(s), 14 second(s))
0 error(s), 0 warning(s) (0 minute(s), 14 second(s))




Tim S.
C Programmer working to learn more about C++.
On Windows 10 64 bit and Windows 11 64 bit.
--
When in doubt, read the CB WiKi FAQ. [url="http://wiki.codeblocks.org"]http://wiki.codeblocks.org[/url]

stahta01

Attached my project with your code.
C Programmer working to learn more about C++.
On Windows 10 64 bit and Windows 11 64 bit.
--
When in doubt, read the CB WiKi FAQ. [url="http://wiki.codeblocks.org"]http://wiki.codeblocks.org[/url]

spartan

Oh, now it works....thanks!
I just have one question:when creating new ccp or header file even if I tick box that says "Add file to active project" I still have to manually add file to my project otherwise same error happens. Only way I can get rid of this error, aside from manually adding file, is to also tick off boxes bellow "Add file to active" project": "Debug" and "Release". Is this correct way to get systemtically rid of my error?

oBFusCATed

In your case you need to have ticks in front of both release and debug in the dialog which is shown up.

If you don't do it the file won't be added to both targets. If you build a target which doesn't include the file then you'll get this undefined reference error.
You could fix this problem in both the Project -> Properties and in File -> Properties dialogs.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]