Undefined reference to in AVR-GCC - c

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.

Related

Codeblocks undefined reference to functions error. Already tried add include directory to project build options

I recently inherited a console application that I do not have the project file for. I only have the executable file, the main.c and a bunch of .h header files. I need to edit the main.c and produce an exe file with my edits.
I created a new C console application project on Codeblocks then pasted the main.c contents from the "hand me down" file onto the new project's main.c.
I just edited a fieldname on the main.c file. A very minor edit.
I pasted the .h files to C:\Program Files\CodeBlocks\MinGW\include.
I go to Project >> Build Options >> Search directories >> Compiler >> Add the above include path (saw this step on codeblocks forum and on stackoverflow)
Now the problem is; I'm getting errors, there are multiple undefined reference to 'functionNames'.
These function names are inside the .h files.
I also included the path not just on Compiler but on Linker and Resource compiler to no avail, still getting the same error.
Some of the include .h on main.c also references other .h files which are also pasted on the CodeBlocks\MinGW\include folder.
The include calls are <> and not "" since they are pasted on mingw\include. These .h files are from an api package.
#include <stdio.h>
#include <windows.h>
#include <global.h>
#include <osfile.h>
#include <mail.h>
.. etc
What am I missing? Thanks!

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

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

is adding a function in another file to a project needs modifying makefile?

Is using a function defined in another file (example.[ch]) which is placed in project directory (and included with #include "example.h") needs editing project Makefile?
i tried using my function in part of net-snmp project and faced linker errors.
the error is:
./.libs/libnetsnmpmibs.so: undefined reference to `snmpget'
the nodeFunc.c look like this:
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>
#include <string.h>
#include "NodeFunc.h"
int snmpget(char* remoteip, short remoteport,char* community,int *len,char* str)
{
....using some net-snmp functions to get a certain object from a remote host....
}
and i used the snmpget function in my main file like this:
...
snmpget(remIP,161,remComm,&lenth,ansstr);
...
It is not the addition of a function that would require modification on the makefile, but rather the addition of the source file itself. If in the makefile the source file is not already referenced as a compilation target and its object file is not a link dependency, then it will not be included in the build.
Inclusion in the build is not the purpose of the #include directive. That merely includes the content of the .h file in the source file in order that the compiler has visibility of the declarations; it does not involve the .c file containing the definitions - that must be separately compiled and linked which is the role of the makefile in this case.

"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

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>

Resources