fatal error: addFunc.h: No such file or directory - c

i have created a Makefile to run C program in shell script.but i get as error fatal error: addFunc.h: No such file or directory in mainProg.c page and addFunc.c page.i tried my best to solve this problem.but i didn't get a solution.i have mentioned my code below.
mainProg.c
#include <stdio.h>
#include "/home/name/Desktop/add/addFunc.h"
int main(){
int a,b;
printf("Enter two numbers\n");
scanf("%d%d",&a,&b);
printf("sum:%d\n",add(a,b));
return 0;
}
addFunc.h
int add(int a, int b)
addFunc.c
#include "/home/name/Desktop/add/addFunc.h"
int add(int a,int b){
return (a+b);
}
Makefile
Add: mainProg.c addFunc.c
gcc -o Add mainProg.c addFunc.c -I.

You really should be cutting and pasting, because clearly the text you've pasted here is nothing like what you're actually using (you have tons of syntax errors here).
However, the problem is that the compiler can't find your header file. This can be solved in one (or both) of two ways:
First, you should use the #include <...> form only for system headers like stdio.h etc. For your personal headers, you should use the form #include "..." instead. The difference is implementation-specific but for most compilers it is that include files using <...> are never looked for in the current directory but only in system directories and directories given with -I, while include file using "..." are looked for also in the current directory.
You could also simply ask the compiler to always look in the current directory by adding the -I. option to your compile line in your makefile.

It seems the problem you are getting is occurring because you are using #include <addFunc.h>. You would use <> when you are using a system header file. From your makefile it seems you are not.
The #include statement is a little different when you use header files of your own program because it searches in the dictionary your .c file is located.
Using #include "addFunc.h" should do the trick.
For more info on the #include directive see this page

Related

"Already defined in object file" and "one or more multiply defined symbols found"

Going through the RPC tutorial at MSDN,
I have just created a project with two .c files as following::
/* file hellop.c */
#include <stdio.h>
#include <windows.h>
void HelloProc(char * pszString)
{
printf("%s\n", pszString);
return ;
}
and
/* file: hello.c */
#include "hellop.c"
void main(void)
{
char * pszString = "Hello, World";
HelloProc(pszString);
return ;
}
Problem:: Error LNK2005 and fatal Error LNK1169
Why and where is the compiler seeing the multiple symbol definition or declaration of HelloProc(char*) ?
EDIT:: As concluded in this SO_Question, including .h file is the best solution obviously. But does that leave us with no implementation of design where we can include a .c file into another .c file?
Weird Behavior:: First time compilation runs fine but rebuild of solution breaks with the above mentioned errors. You can check the multiple first time compilation by changing the file name from .c to .cpp and vice-versa. Why does it exhibit this behavior? (I am not sure if anybody else have also experienced this with the given example)
You compiling HelloProc twice, as you include the whole definition of of this function in hello.c file by #include "hellop.c", while you only need declaration of it. You should put function prototype in header file.
#ifndef HELLOP_H
#define HELLOP_H
#include <stdio.h>
#include <windows.h>
void HelloProc(char * pszString);
#endif
And include header file both in hellop.c and in hello.c
Edit: #include is not cut-paste as you said, it is more copy-paste
# include "q-char-sequence" new-line
causes the replacement of that directive by the entire contents of the
source file identified by the specified sequence between the "
delimiters
so you get to linkage with two definitions of HelloProc one in hellop.c and another one in hello.c. Another way to solve it is to compile only hello.c file, this way there is no duplicate of HelloProc. See how to do it in VisualStudio here

How to link two source files properly? undefined reference error

Ok so I am doing a final project for one of my classes and trying to do a bit extra and create multiple files to work with. I am coding inside of CodeBlocks. So far I have a main.c, levels.c, and levels.h for my files. Inside of the levels.c levelOne function, I put the printf statement as a test to make sure I could have the two files work with each other before I went forward in my coding. I got a "undefined reference to 'levelOne' when I compiled and ran the program.
Inside my main.c file:
#include <stdio.h>
#include <stdlib.h>
#include "levels.h"
int main()
{
levelOne();
return 0;
}
Inside my levels.h file:
#ifndef LEVELS_H_INCLUDED
#define LEVELS_H_INCLUDED
void levelSelect(char c);
void levelOne();
void levelTwo();
void levelThree();
void levelCustom(int difficulty);
#endif // LEVELS_H_INCLUDED
Inside my levels.c file:
void levelOne()
{
//level scope of 1 to 10
srand(time(NULL));
int randomNum = (rand() % 9)+1);
printf("the random number is: %i\n", randomNum);
}
levels.c is not getting passed into the compiler, are you sure you have included levels.c in the whole project? If not it will not link. You need a project if you want to compile multiple files. In CodeBlocks, the sources and the settings for the build process are stored in a project file <name>.cbp
Here is the User Manual
gcc levels.c main.c should link successfully. gcc main.c will only compile one file and try and link to create final executable and levelOne() will not be found. since it is in file levels.c
You need to include levels.h in levels.c as well or if a function (physically) above levelOne calls it, it is undefined.
Then compile it with gcc -Wall *.c -o myapp to compile and link all of the c files in that directory into myapp (or you can name them individually) with (almost) all warnings enabled. This is provided you have it in its own directory.
Once you get into larger projects with more code, you can compile individual .c files into .o object files with gcc -Wall -c somecode.c and then link all the objects with gcc *.o -o myapp. If it gets really large, you'll want a build system to help with rebuilding objects only when its code (or dependent code) changes (such as Makefiles, waf, and dare I say autotools).
I had this exact same problem, the solution is easy. Right click on levels.c and select properties. A properties window should come up select the "Build" tab tick compile file, link file, and in the box check debug and release. This should fix your problem.
Don't make the mistake of doing this with a header file because it will give you a "...h.gch: file not recognized: File format not recognized.." error.

Undefined reference to in AVR-GCC

My main.c is as below
#include <avr/io.h>
#include<avr/interrupt.h>
#include<util/delay.h>
#include <string.h>
#include "main.h"
#include "globle.h"
#include "LCD.h"
int main()
{
...
...
...
lcdInit(0xc0);
lcdScreen(0);
.
.
.
return 0;
}
The definition of lcdInit(0xc0); and lcdScreen(0); is in my lcd.c file
and I have a header file lcd.h having the following lines:
void lcdInit(char);
void lcdScreen(char);
But still I am getting:
C:\Documents and Settings\Tanv\My Documents\my_project5\default/../Main.c:95: >undefined >reference to
`lcdInit'
and
C:\Documents and Settings\Tanvr\My
Documents\my_project5\default/../Main.c:96: undefined reference to
`lcdScreen'
What is wrong here?
This is a linker error.
You are not building your program properly, you need to compile all C files together, like so:
$ gcc-avr -o program main.c lcd.c
or link them together from object files if you compile separately.
Add source and header files to your project by
1. Right click "Source Files" then "Add Existing Source File(s)"
2. Right click "Header Files" then "Add Existing Header File(s)"
Refer to Add Source to Project Step 6.
I had the same problem and I added the files to the project from the beginning and they were compiled together. But this did not solve the problem and I accidentally noticed that I made a mistake, namely the file of the implementation of the functions of the library had the extension .c, and the main extension .cpp. To solve the problem, I simply reassembled the project in c format.

embed ruby code in C

I know there's severals post about this, but i'm stack
here's my C code
#include </usr/include/ruby-1.9.1/ruby/ruby.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
ruby_init();
rb_eval_string("puts 'hello'");
ruby_finalize();
return 0;
}
i've got the following error when compile it in sublime text 2
In file included from /Users/pierrebaille/Code/Ruby/embedRuby/embedRubyFirst.c:1:
/usr/include/ruby-1.9.1/ruby/ruby.h:1481:24: error: ruby/subst.h: No such file or directory
[Finished in 0.1s with exit code 1]
thanks for your help
You should not hard-code the full path of a header file like
#include </usr/include/ruby-1.9.1/ruby/ruby.h>
proper is
#include <ruby.h>
and told your gcc to search the header file via CFLAGS and libariy via LD_FLAGS, simply command without makefile could be:
gcc -o demo.exe -I/path/to/ruby/headers rubydemo.c -L/path/to/ruby/lib -lruby-libary-name
One of you files you're including in turn includes ruby/subst.h, , but it appears that ruby is not in your path, which is why you have this in your code:
#include </usr/include/ruby-1.9.1/ruby/ruby.h>
Instead of hardcoding paths you should simply add "/some_path/" to your compiler path(s) setting, where some_path contains the folder ruby as a child. Now your own include turns into:
#include <ruby/ruby.h>

Undefined reference error in C using Code::Blocks IDE

I'm trying to set up some .c files to make it easier on me to find things, once it starts becoming larger. I'll be using SDL calls in the program, hence the includes.
Here's how my main.cpp looks right now:
#include <stdio.h>
#include <SDL.h>
#include <SDL_gfxPrimitives.h>
#include <SDL_ttf.h>
#include "WriteText.h"
int main(int argc, char *argv[])
{
int i =0;
i = b();
return 0;
}
In my WriteText.c I have:
#include "WriteText.h"
int b(void)
{
return 3;
}
Finally my WriteText.h:
#ifndef WRITETEXT_H_INCLUDED
#define WRITETEXT_H_INCLUDED
int b(void);
#endif // WRITETEXT_H_INCLUDED
Trying to compile it, I get an undefined reference to 'b()'. I have no idea why this is happening, I practiced it in some basic example codes and everythin works just fine, but as soon as I'd actually use it for something practical I hit an error like this.
The problem is that you are not linking the WriteText.c into your executable. If you gave some more information about how you are creating the executable we could probably give better help.
Chances are your compilation isn't using the writetext object file. Assuming *nix and gcc, your makefile should look something like:
all: myprog
myprog: myprog.o writetext.o
gcc -o $# $^
myprog.o: myprog.cpp
writetext.o: writetext.cpp
I figured out my issue, my main file was .cpp and used CPP compiler (because it was an SDL project), but the new file I added was .c and used C compiler, when I added a new .cpp file and just copy-pasted my code into it, everything worked smoothly.
(I'm not sure if it's "bad form" to write C code in .cpp files but if I intend to use SDL I guess that's the only way to go.)

Resources