Author Topic: How to work with header files in codeblocks  (Read 10295 times)

ankitbehera2670

  • Guest
How to work with header files in codeblocks
« on: February 23, 2018, 09:39:43 am »
I have three file:

main.cpp:
Code
#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:
Code
#ifndef _HEAD_H_
#define _HEAD_H_
int sum(int a , int b);
#endif
head.c:
Code
#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.

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 3353

ankitbehera2670

  • Guest
Re: How to work with header files in codeblocks
« Reply #2 on: February 23, 2018, 11:24:22 am »
No. I am using projects. But I did choose "Empty  Project".
« Last Edit: February 23, 2018, 11:30:22 am by ankitbehera2670 »

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: How to work with header files in codeblocks
« Reply #3 on: February 23, 2018, 11:54:00 am »
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

  • Guest
Re: How to work with header files in codeblocks
« Reply #4 on: February 23, 2018, 12:45:59 pm »
I have already done that but still doesn't seem to work. Here is screenshot:https://ibb.co/cKX5xc

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 3353
Re: How to work with header files in codeblocks
« Reply #5 on: February 23, 2018, 02:01:17 pm »
how about you read my link and do what is written there? without this information this is wild guessing....

ankitbehera2670

  • Guest
Re: How to work with header files in codeblocks
« Reply #6 on: February 23, 2018, 05:44:12 pm »
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)' ".

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 3353
Re: How to work with header files in codeblocks
« Reply #7 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?

Online stahta01

  • Lives here!
  • ****
  • Posts: 7582
    • My Best Post
Re: How to work with header files in codeblocks
« Reply #8 on: February 23, 2018, 10:28:47 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++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org