Codeblocks undefined reference to ошибка

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's user avatar

asked Sep 17, 2013 at 5:13

Michael harris's user avatar

Michael harrisMichael harris

9662 gold badges14 silver badges25 bronze badges

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!

answered Sep 17, 2013 at 5:54

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 ?

Pranav 웃's user avatar

Pranav 웃

8,4696 gold badges38 silver badges48 bronze badges

answered Sep 17, 2013 at 5:23

Some programmer dude's user avatar

8

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!

answered Dec 1, 2014 at 2:56

Kaz's user avatar

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

answered Oct 3, 2015 at 6:03

dmytro.poliarush's user avatar

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

answered May 23, 2018 at 13:46

Charles's user avatar

CharlesCharles

1,8441 gold badge14 silver badges21 bronze badges

Topic: undefined reference  (Read 17242 times)

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))

« Last Edit: November 03, 2017, 07:28:03 pm by spartan »


Logged


« Last Edit: November 03, 2017, 07:50:03 pm by stahta01 »


Logged

C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.

When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org


Still waiting for the full build log!


Logged

C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.

When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org


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))

« Last Edit: November 03, 2017, 08:24:21 pm by spartan »


Logged


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.

« Last Edit: November 03, 2017, 08:34:36 pm by stahta01 »


Logged

C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.

When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org


Attached my project with your code.


Logged

C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.

When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org


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?


Logged


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.


Logged

(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!]


04-20-2013


#1

Click_here is offline


TEIAM — problem solved


Code::blocks ‘undefined reference’ error

I am using Code::Blocks 12.11 on Windows 8

I am following the tutorial here — Lesson 2 — Creating a basic window — Natural Language Processing, London and I am receiving the error «undefined reference to ‘MainWindow::m_hInstance'» in the MainWindow.cpp file. I don’t know enough about the C++ language to guage whether there is an error in the code, or (more likely) it’s an error in the Code::Blocks project set-up.

I have three files: main.cpp, MainWindow.cpp, MainWindow.h

main.cpp

Code:

#include <windows.h>
#include "MainWindow.h"

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine, int nCmdShow)
{
    MSG msg;

    MainWindow *winMain = new MainWindow(hInst);
    if(!winMain->Run(nCmdShow))
    {
        delete winMain;
        return 1; 
    }

    // Run the message loop. It will run until GetMessage() returns 0
    while (GetMessage (&msg, NULL, 0, 0))
    {
        // Translate virtual-key messages into character messages
        TranslateMessage(&msg);
        // Send message to WindowProcedure
        DispatchMessage(&msg);
    }

    delete winMain;

    return msg.wParam;
}

MainWindow.cpp

Code:

#include <windows.h>
#include "MainWindow.h"

char MainWindow::m_szClassName[] = "DrawLite";

MainWindow::MainWindow(HINSTANCE hInstance)
{
    m_hInstance = hInstance;
  
    m_wndClass.cbSize = sizeof(WNDCLASSEX);
    m_wndClass.style = CS_DBLCLKS;
    m_wndClass.lpfnWndProc = MainWndProc;
    m_wndClass.cbClsExtra = 0;
    m_wndClass.cbWndExtra = 0;
    m_wndClass.hInstance = hInstance;
    m_wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    m_wndClass.hCursor = LoadIcon(NULL, IDC_ARROW);
    m_wndClass.hbrBackground = (HBRUSH) COLOR_WINDOW;
    m_wndClass.lpszMenuName = NULL;
    m_wndClass.lpszClassName = m_szClassName;
    m_wndClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

}

MainWindow::~MainWindow()
{

}

LRESULT CALLBACK MainWindow::MainWndProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg)
    {
    case WM_DESTROY:
        PostQuitMessage (0);
        break;
    default:
        return DefWindowProc (hwnd, msg, wParam, lParam);
    }

    return 0;
}

bool MainWindow::Run(int nCmdShow)
{
    if(!RegisterClassEx(&m_wndClass))
        return false;
    m_hwnd = CreateWindowEx(
                 0,
                 m_szClassName,
                 "Draw Lite",
                 WS_OVERLAPPEDWINDOW,
                 CW_USEDEFAULT,
                 CW_USEDEFAULT,
                 500,
                 400,
                 NULL,
                 NULL,
                       m_hInstance,      
                 NULL
             );
    if(!m_hwnd)
        return false;
    ShowWindow(m_hwnd, nCmdShow);
    return true;
}

MainWindow.h

Code:

#ifndef MAINWINDOW_H_INCLUDED
#define MAINWINDOW_H_INCLUDED

#include <windows.h>

class MainWindow
{

public:
    MainWindow(HINSTANCE hInstance);
    ~MainWindow();

    static LRESULT CALLBACK MainWndProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
    bool Run(int nCmdShow);

private:
    WNDCLASSEX m_wndClass;
    static HINSTANCE m_hInstance;
    HWND m_hwnd;
    static char m_szClassName[];

};

#endif // MAINWINDOW_H_INCLUDED

I have searched Google for undefined reference problems and have found a few posts which were solved by adding the files to the project — I have added the files by right clicking the project and choosing «add files» and making sure all the boxes are ticked.

I have also tried to add the project directry to the linker tab as described in the Codeblocks manual, and then tried to add the object file in the same way.

I have got the feeling that it is an error in the project set-up, but I’m out of ideas.

Fact — Beethoven wrote his first symphony in C


04-21-2013


#2

Salem is offline


and the hat of int overfl

Salem's Avatar


> I have three files: main.cpp, MainWindow.cpp, MainWindow.h
I believe that all you should need is in project->settings->source files, you just have the two .cpp files listed.

So when you do a full build, you should see two compile lines (for each of the .cpp files), then the linker producing the executable.


04-21-2013


#3

Click_here is offline


TEIAM — problem solved


There doesn’t seem to be a «settings» under the project menu (at least with the version 12.11 that I am running) — However, there is a «Properties» option and the tab «Build Targets». All three files are in the «Build Target Files» and all are selected.

It looks like the two files have been included in the compiler input — Here is the output from the ‘Build Log’

Code:

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


mingw32-g++.exe  -o bin\Debug\Win32Tuturial.exe obj\Debug\main.o obj\Debug\MainWindow.o    

obj\Debug\MainWindow.o: In function `ZN10MainWindowC2EP11HINSTANCE__':

D:/Programming/Win32Tuturial/MainWindow.cpp:8: undefined reference to `MainWindow::m_hInstance'

obj\Debug\MainWindow.o: In function `ZN10MainWindow3RunEi':

D:/Programming/Win32Tuturial/MainWindow.cpp:61: undefined reference to `MainWindow::m_hInstance'

collect2.exe: error: ld returned 1 exit status

Process terminated with status 1 (0 minutes, 0 seconds)

2 errors, 0 warnings (0 minutes, 0 seconds)

Fact — Beethoven wrote his first symphony in C


04-21-2013


#5

Click_here is offline


TEIAM — problem solved


That seemed to work, thanks ZuK.

What is the reason why this needs to be done?

As I said, I don’t know enough about C++ to judge if there was an error in the code.

Fact — Beethoven wrote his first symphony in C


04-22-2013


#7

Click_here is offline


TEIAM — problem solved


Thanks Elysia

So just to clarify:

A Static variable in a class is implemented as one variable which is availible to all classes
i.e. winMain1.m_hInstance and winMain2.m_hInstance are the same variable (same memory location)

A Static variable in a class must be declared in the same way a function is declared for a class. However, a non-static variable does not need to be declared in this way.

Fact — Beethoven wrote his first symphony in C


04-23-2013


#9

Click_here is offline


TEIAM — problem solved


What do you mean by «translation unit», laserlight?

This is my current understanding:
A function can be fully defined in a class definition or external to that class definition. However, a static variable needs to be declared like a global variable, unless it is a constant integer where an exception to the rule is made on the event that a definition was imediately made.

Code:

class apple
{
  /* No further defining is needed here: */
  int banana;
  const static int grape = 4;
  apple();
  ~apple();

  /* These need to be defined again globally */
  const static int pear;
  static char pineapple[42];
  
};

Fact — Beethoven wrote his first symphony in C


04-24-2013


#15

Click_here is offline


TEIAM — problem solved


Quote Originally Posted by Elysia

You should, if possible, use std::string for all your strings, not char arrays.

This tutorial uses some C string functions later on (wsprintf in particular). I might go back through the tutorial’s code later and try to learn and implement some of the C++’s string functionality.

Quote Originally Posted by Elysia

Now that I think about it, why are you using operator new here? It seems superfluous. You should probably remove it.

I am following a tutorial which has chosen to dynamically allocate the class — In fact the WinMain function was so simple, I just copied and pasted from the tutorial without a second thought. I’m not going to change it for changes sake.

Quote Originally Posted by Aeoskype

try adding

at the end of program.

When programming a graphical windows based application, the entry point WinMain instead of main — Adding «main» after the WinMain is already in the code would be a bad choice.

Here it is on the MSDN website — WinMain entry point (Windows)

I think that the tutorial I have been following skims over quite a lot, but through using Google, I’m finding it quite good. However, these tutorials, as well as the MSDN site, have been heavily critisized for not using modern C++ and using C string functions (which is fine for me, because I know C very well, but not C++).

Fact — Beethoven wrote his first symphony in C


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


Recommended Answers

You need to link «gdi32.dll» into your project :)

Chris

Jump to Post

All 4 Replies

Member Avatar for Freaky_Chris


You need to link «gdi32.dll» into your project :)

Chris

Member Avatar for h3llpunk


where can i set that link and where do i get gdi32.dll ??

Member Avatar for Freaky_Chris


Project -> Build Options -> Linker -> add -> gdi32

Chris

Member Avatar for h3llpunk


thx will marking this as solved and hope this will help me for future references


Reply to this topic

Be a part of the DaniWeb community

We’re a friendly, industry-focused community of developers, IT pros, digital marketers,
and technology enthusiasts meeting, networking, learning, and sharing knowledge.

Понравилась статья? Поделить с друзьями:

Интересное по теме:

  • Code leaf apex legends ошибка
  • Codesys ошибка соединения no object
  • Code language not supported or defined ошибка
  • Code ds42019 ошибка
  • Codesys ошибка 4020

  • 0 0 голоса
    Рейтинг статьи
    Подписаться
    Уведомить о
    guest

    0 комментариев
    Старые
    Новые Популярные
    Межтекстовые Отзывы
    Посмотреть все комментарии