Code::Blocks

User forums => General (but related to Code::Blocks) => Topic started by: ankitbehera2670 on February 23, 2018, 09:39:43 am

Title: How to work with header files in codeblocks
Post by: ankitbehera2670 on February 23, 2018, 09:39:43 am
I have three file:

main.cpp:
Code: [Select]
#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: [Select]
#ifndef _HEAD_H_
#define _HEAD_H_
int sum(int a , int b);
#endif
head.c:
Code: [Select]
#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.
Title: Re: How to work with header files in codeblocks
Post by: BlueHazzard on February 23, 2018, 11:20:33 am
http://wiki.codeblocks.org/index.php?title=FAQ-Compiling_(general)#Q:_How_do_I_report_a_compilation_problem_on_the_forums.3F

but probably you are not using projects?
Title: Re: How to work with header files in codeblocks
Post by: ankitbehera2670 on February 23, 2018, 11:24:22 am
No. I am using projects. But I did choose "Empty  Project".
Title: Re: How to work with header files in codeblocks
Post by: oBFusCATed on February 23, 2018, 11:54:00 am
Then add all your files to the project. This will resolve the problem.
Title: Re: How to work with header files in codeblocks
Post by: ankitbehera2670 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
Title: Re: How to work with header files in codeblocks
Post by: BlueHazzard 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....
Title: Re: How to work with header files in codeblocks
Post by: ankitbehera2670 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)' ".
Title: Re: How to work with header files in codeblocks
Post by: 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?
Title: Re: How to work with header files in codeblocks
Post by: stahta01 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.