undefined reference with codeblocks

hi guys,

so there is no problem I am looking to solve as the problem has already been solved but just noticed a strange occurrence with the codeblocks ide,so I have two files one .cpp and one.h file one is cordinates.cpp and cordinates.h(yes spelled wrong to avoid possible name clashes,I'm sure a class called coordinates probably exists already,probably should have made a namespace but meh)

any who I included the cordinates.h into my main cpp file where main is and I got plenty of undefined references to functions such as solve and the constructor,so I decided to also include cordinates.cpp in my main cpp file and it worked,

this is quite surprising to me as I would have been under the impression that once the header is included and the .cpp file is in local scope of the project it should find the code but looks like it didn't

I wonder why?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

#include <iostream>
#include <sstream>
#include "cordinates.h"
#include "cordinates.cpp"

using namespace std;

int main()
{

    cordinates point1(2,"5x + 3x + 2x");
    int one = point1.solve();

    cordinates point2(3,"5x + 3x + 2x");
    int two = point2.solve();

    cordinates point3(4,"5x + 3x + 2x");
    int three = point3.solve();

    cout << "P 1 : " << one << endl;
    cout << "P 2 : " << two << endl;
    cout << "P 3 : " << three << endl;
    return 0;
}
You should't need to do that.
1. Is the .cpp file a part of your CB project?
2. Are you defining templates at all?

It's hard to tell without seeing the code. If the code is annoyingly long, just make a dummy constructor and solve method to reproduce the same error.
Sounds like you haven't added cordinates.cpp as part of the project.

The way it works is that each source file (.cpp) is compiled independently, without the knowledge of other source files. When all the source files has been compiled everything is linked together to create the executable file.

If cordinates.cpp is not part of the project it would notice that some of the definitions are missing and give you a linker error "undefined reference".

There is no magic relationship between source files and header files. That they are named the same is only for our own sanity. You could call the header file asda.h and the source file xc76.cpp if you wanted.
Do not include the cpp-files. While it might succeed, it is not what is expected.

The "include" directive is almost like copy-pasting the content of the named file into your code. In other words, you have all your code in "one file".


What you should have done is to tell the codeblocks that your "project" contains two .cpp files. When you have done that properly, the "build" will.

1. compile the cordinates.cpp into cordinates.o
2. compile the main.cpp into main.o
3. link cordinates.o, main.o, and standard libraries into the executable file


How to use your IDE? Read its documentation. Consult its Forum.
I didn't add a class to the project instead I manually added a .cpp file and a .h file instead of choosing to add a class maybe that has something to do with it?

Sounds like you haven't added cordinates.cpp as part of the project.

The way it works is that each source file (.cpp) is compiled independently, without the knowledge of other source files. When all the source files has been compiled everything is linked together to create the executable file.


that could exactly be the problem as what I said above may be the reason for it?

thanks guys

What do you mean by "manually" adding a .cpp and .h file?
In CB, did you right click the project and do "add file" or whatever the equivalent is?

Might want to post the contents of your .cbp file (open in notepad or equivalent).

Post the compilation command that CB is generating, as well. I forget where exactly it shows you it, but it should be on the build output window.
Last edited on
by manually I meant I selected the project and instead of selecting add class I chose add file > add c/c++ header and add c/c++ source file

mingw32-g++.exe -o bin\Release\cordinateMath.exe obj\Release\main.o -s -lmingw32 -lSDL2 -lSDL2main "C:\Program Files (x86)\CodeBlocks\MinGW\lib\libSDL2.dll.a" "C:\Program Files (x86)\CodeBlocks\MinGW\lib\libSDL2main.a" this is with cordinates.cpp included

this is with just cordinates.h

mingw32-g++.exe -o bin\Release\cordinateMath.exe obj\Release\main.o -s -lmingw32 -lSDL2 -lSDL2main "C:\Program Files (x86)\CodeBlocks\MinGW\lib\libSDL2.dll.a" "C:\Program Files (x86)\CodeBlocks\MinGW\lib\libSDL2main.a"

here is the undefined reference errors

obj\Release\main.o:main.cpp:(.text+0x21f): undefined reference to `cordinates::cordinates(int, std::string)'
obj\Release\main.o:main.cpp:(.text+0x25f): undefined reference to `cordinates::solve()'
obj\Release\main.o:main.cpp:(.text+0x270): undefined reference to `cordinates::getX()'
obj\Release\main.o:main.cpp:(.text+0x2b5): undefined reference to `cordinates::cordinates(int, std::string)'
obj\Release\main.o:main.cpp:(.text+0x2f5): undefined reference to `cordinates::solve()'
obj\Release\main.o:main.cpp:(.text+0x306): undefined reference to `cordinates::getX()'
obj\Release\main.o:main.cpp:(.text+0x34b): undefined reference to `cordinates::cordinates(int, std::string)'
obj\Release\main.o:main.cpp:(.text+0x38b): undefined reference to `cordinates::solve()'
obj\Release\main.o:main.cpp:(.text+0x39c): undefined reference to `cordinates::getX()'
obj\Release\main.o:main.cpp:(.text+0x3a7): undefined reference to `cordinates::getY()'
obj\Release\main.o:main.cpp:(.text+0x5a1): undefined reference to `cordinates::solve()'
obj\Release\main.o:main.cpp:(.text+0x5b9): undefined reference to `cordinates::getX()'
obj\Release\main.o:main.cpp:(.text+0x644): undefined reference to `cordinates::cordinates(int, std::string)'
obj\Release\main.o:main.cpp:(.text+0x6a1): undefined reference to `cordinates::solve()'
obj\Release\main.o:main.cpp:(.text+0x6b9): undefined reference to `cordinates::getX()'
obj\Release\main.o:main.cpp:(.text+0x744): undefined reference to `cordinates::cordinates(int, std::string)'
C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/4.9.2/../../../../mingw32/bin/ld.exe: obj\Release\main.o: bad reloc address 0x3c in section `.rdata'
C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/4.9.2/../../../../mingw32/bin/ld.exe: final link failed: Invalid operation
collect2.exe: error: ld returned 1 exit status

At least that confirms that it's only linking main.o and nothing calling "cordinates.o" or similar.
I don't know why CB isn't trying to compile your cordinates.cpp file. Maybe, for some reason, that file isn't set to be compiled in Release mode. But that's just a guess.

Can you post the contents of your CBP ("Code Blocks Project") file?
sure Ganado,

this may sound dumb but where can I find this file? or do you mean the content of the actual source files?

thanks
here is the contents from the cbp file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
	<FileVersion major="1" minor="6" />
	<Project>
		<Option title="cordinateMath" />
		<Option pch_mode="2" />
		<Option compiler="gcc" />
		<Build>
			<Target title="Release">
				<Option output="bin/Release/cordinateMath" prefix_auto="1" extension_auto="1" />
				<Option object_output="obj/Release/" />
				<Option type="1" />
				<Option compiler="gcc" />
				<Compiler>
					<Add option="-O2" />
				</Compiler>
				<Linker>
					<Add option="-s" />
				</Linker>
			</Target>
		</Build>
		<Compiler>
			<Add option="-Wall" />
			<Add option="-fexceptions" />
		</Compiler>
		<Unit filename="main.cpp" />
		<Extensions>
			<code_completion />
			<envvars />
			<debugger />
		</Extensions>
	</Project>
</CodeBlocks_project_file>
Last edited on
What's going on here is that you still, STILL haven't learned how C++ goes from files to objects to executables and libraries.

You've been here for two and a half years. You've posted almost a thousand times.

Stop procrastinating and learn it.
... If you are using windows, it is common for the system to set hidden extensions by default, you can easily change that with a google, all programmers usually have this option on. (sorry if I am wrong, but you should know that the cbp file is the file that you use to open your project), otherwise if you just don't know where it is you can right click on your project in the left column and click "open project from file explorer".

The reason why it doesn't get compiled is due to codeblocks always asking you which build targets would you like the file to be included with, and you forgot to check all build targets by accident.

In the properties of the project, you can look under build targets tab, and you can check if the files are checked in.

If you have trouble with finding that, you can also just remove the cordinate.cpp file from the project and re include it, that could fix it.


I assumed that you had the file inside the project, which should be obvious if you look at the files in your project in the left side column. I thought you said in your post that you created the file from new>file>h and cpp for both the cordinates files, but you didn't. You can include files by right clicking the project and adding files. You would also do yourself a favor if you had a debug and release build, since you should be frequently going to debug mode for finding segment faults and other bugs.

Also note that you are manually pointing to libSDL2.dll.a instead of having SDL linked up with -L"C:/..." to the folders instead, which confuses me why -lSDL2 -lSDL2main works at all, since the -l options are supposed to lead to the libLIBRARYNAME.a(.dll) magically, like -lmingw32 points to libmingw32.a which is always in the scope because its inside of the compiler scope. It is possible that SDL2 could be in the scope due to putting it in the global PATH or a global IDE variables (and other scope possibilities).

I would be surprised if it is true that if you can link to the file directly while linking the same library with linker flags at the same time and not have any problems, and I just tested it and it is true you can link the same thing over and over again without warning, I hope the linker is smart enough to make it indifferent from just linking once (it should since it would have redefinition errors otherwise).... It isn't a big problem, but I like having all my things nice and strict, and having a warning would be nice. At least codeblocks is smart enough to automatically cull link flag duplicates the next time you look at them, but it wont clean up your scenario.
Last edited on
I have both Debug and Release configurations on mine, although you having only Release should still be fine. But for some reason, your only "Unit filename" is main.cpp. This means you aren't actually adding the other files to your project. The main.cpp, cordinate.cpp, cordinate.h, and my project are all in the same folder, and I did right-click --> Add File for each one, and added each one to Debug and Release.

My built output looks like this:

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

mingw32-g++.exe -Wall -fexceptions -g  -c C:\dev\cordinateMath\cordinate.cpp -o obj\Debug\cordinate.o
mingw32-g++.exe -Wall -fexceptions -g  -c C:\dev\cordinateMath\main.cpp -o obj\Debug\main.o
mingw32-g++.exe  -o bin\Debug\cordinateMath.exe obj\Debug\cordinate.o obj\Debug\main.o   
Output file is bin\Debug\cordinateMath.exe with size 1.51 MB
Process terminated with status 0 (0 minute(s), 0 second(s))
0 error(s), 0 warning(s) (0 minute(s), 0 second(s))


My CodeBlocks project file looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
	<FileVersion major="1" minor="6" />
	<Project>
		<Option title="cordinateMath" />
		<Option pch_mode="2" />
		<Option compiler="gcc" />
		<Build>
			<Target title="Debug">
				<Option output="bin/Debug/cordinateMath" prefix_auto="1" extension_auto="1" />
				<Option object_output="obj/Debug/" />
				<Option type="1" />
				<Option compiler="gcc" />
				<Compiler>
					<Add option="-g" />
				</Compiler>
			</Target>
			<Target title="Release">
				<Option output="bin/Release/cordinateMath" prefix_auto="1" extension_auto="1" />
				<Option object_output="obj/Release/" />
				<Option type="1" />
				<Option compiler="gcc" />
				<Compiler>
					<Add option="-O2" />
				</Compiler>
				<Linker>
					<Add option="-s" />
				</Linker>
			</Target>
		</Build>
		<Compiler>
			<Add option="-Wall" />
			<Add option="-fexceptions" />
		</Compiler>
		<Unit filename="cordinate.cpp" /> <!-- COPY THESE (Delete this comment) -->
		<Unit filename="cordinate.h" />
		<Unit filename="main.cpp" />
		<Extensions>
			<code_completion />
			<envvars />
			<debugger />
			<lib_finder disable_auto="1" />
		</Extensions>
	</Project>
</CodeBlocks_project_file>


Try copying the above project file. Copy your old CBP file as a backup in case something goes wrong.

Also, try clearing your codeblocks settings. See: http://forums.codeblocks.org/index.php?topic=3088.0
You could do "codeblocks.exe --clear-configuration" (from the command line)

(Might be outdated info, I dunno).

If all else fails, make a new empty project and copy + add the files in again.

Last edited on
> here is the contents from the cbp file
you had -lSDL2 -lSDL2main in the build command, ¿where are they?
Last edited on
Topic archived. No new replies allowed.