Code::Blocks Forums

User forums => Using Code::Blocks => Topic started by: spartan on November 03, 2017, 04:19:06 pm

Title: undefined reference
Post by: spartan on November 03, 2017, 04:19:06 pm
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))
 
Title: Re: undefined reference
Post by: stahta01 on November 03, 2017, 07:21:01 pm
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 (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.
Title: Re: undefined reference
Post by: stahta01 on November 03, 2017, 08:02:06 pm
Still waiting for the full build log!
Title: Re: undefined reference
Post by: spartan on November 03, 2017, 08:18:37 pm
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))


 
Title: Re: undefined reference
Post by: stahta01 on November 03, 2017, 08:26:26 pm
Add the other source file to the project!

Build -> Rebuild gives me this as the full rebuild log.
Code
-------------- 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.
Title: Re: undefined reference
Post by: stahta01 on November 03, 2017, 08:40:53 pm
Attached my project with your code.
Title: Re: undefined reference
Post by: spartan on November 03, 2017, 08:59:50 pm
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?
Title: Re: undefined reference
Post by: oBFusCATed on November 03, 2017, 09:05:55 pm
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.