News:

The new Release 25.03 is out! You can download binaries for Windows and many major Linux distros here .

Main Menu

How to work with header files in codeblocks

Started by ankitbehera2670, February 23, 2018, 09:39:43 AM

Previous topic - Next topic

ankitbehera2670

I have three file:

main.cpp:
#include <iostream>
#include "head.h"
using namespace std;

int main()
{
    int a,b;
    a=2;b=3;
    int c=sum(a,b);
    cout <<"c:"<<c<<endl;
    return 0;
}


head.h:
#ifndef _HEAD_H_
#define _HEAD_H_
int sum(int a , int b);
#endif

head.c:
#include "head.h"

int sum(int a, int b)
{
    return(a+b);
}


When I compile it, codeblocks give me the error: undefined error to function sum. And I have also included the path in Setting->Build Options->Search Directories->Compiler->Add. But still I get the error.


ankitbehera2670

#2
No. I am using projects. But I did choose "Empty  Project".

oBFusCATed

Then add all your files to the project. This will resolve the problem.
(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!]

ankitbehera2670

I have already done that but still doesn't seem to work. Here is screenshot:https://ibb.co/cKX5xc

BlueHazzard

how about you read my link and do what is written there? without this information this is wild guessing....

ankitbehera2670

My problem is solved. It looks like the problem was related to the installation of code blocks but I was able to get the desired result after uninstalling and reinstalling codeBlocks. However the following are the details:

I am running codeBlocks 16.01 on ubuntu 16.04.9. The compiler I use is g++ version 5.4.0.
I have 3 files that I want to compile: main.cpp head.h head.cpp.
I had created a new empty project in codeblocks and then I had created these 3 files within codeblocks. I have also set the path in Setting->Build Options->Search Directories->Compiler->Add. However when I click the Build and Run button, codeblocks gives me the error that "Undefined reference to function 'sum(int, int)' ".

BlueHazzard

nice that it works... But for the next time: In the link is clearly described that we need a full rebuild log. Without this log it is impossible to help. Can  people not read and follow instructions nowadays?

stahta01

Quote from: BlueHazzard on February 23, 2018, 09:16:39 PM
nice that it works... But for the next time: In the link is clearly described that we need a full rebuild log. Without this log it is impossible to help. Can  people not read and follow instructions nowadays?

I am actually wondering if they can follow links, sometimes.

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]