Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm having a couple of issues including custom libraries in my program
I have my main.c file and a library.c (where all the functions are stored) and library.h (where all the prototypes are stored).
In main.c I'm placing #include "library.h" but the functions don't get recognized when I try to compile.
Am I doing something wrong?
I'm using GCC to build the file.
test.c:
#include "library.h"
int main()
{
int num = 5;
sum(num);
}
library.c
#include "library.h"
int sum(int num)
{
return num + 5;
}
library.h
#ifndef LIBRARY_H
#define LIBRARY_H
#include <stdio.h>
int sum(int num);
#endif
Getting error:
C:\Users\Gabriel\Desktop\test.o:test.c|| undefined reference to `sum'|
Including the header file is not sufficient. The prototype in the header file just tells the compiler that there should be a function.
You have to actually add the function to the program. That is done when linking. the simplest way would be
gcc -o myprog test.c library.c
There are more sophisticated option. If you want to add several files actually to a library you can compile them independently and build the archive. Here are some commmands that show the basic idea.
gcc -o library.o library.c
gcc -o someother.o someother.c
ar a libmy.a library.o someother.o
gcc -o myprog test.c -l my
Related
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 9 months ago.
I'm attempting to make a custom header in C for user input. Whenever I try to use it, it throws the error:
"Undefined Reference to (FUNCTION_NAME)"
It's in the same folder as all the other header files and vscode doesn't have a problem finding the header file. Both the .h and .c are in the same folder.
Here is the code in the header, c file, and program I'm using:
SGGINPUT.h
#ifndef SGGINPUT
#define SGGINPUT
#include <stdio.h>
char SGGchar();
#endif
SGGINPUT.c
#include <stdio.h>
#include "SGGINPUT.h"
char SGGchar() {
char c;
printf("Enter Char: ");
scanf("%c",&c);
printf("\n");
return c;
}
test.c
#include <stdio.h>
#include "SGGINPUT.h"
int main() {
char c = SGGchar();
printf("%c",c);
}
Try:
gcc -c SGGINPUT.h SGGINPUT.c
A result file will be an obj file:
SGGINPUT.o
Next compile it this way:
gcc -g SGGINPUT.o test.c -o "Output_name"
Plus you don't really need to include <stdio.h> in your header, you are just declaring your functions there and nothing more.
My solution was:
gcc -c test.c
gcc -c 'C:\msys64\mingw64\include\SGGINPUT.c'
gcc -o test test.o SGGINPUT.o
And then run the executable.
So this solution works if I were to make o files in the same directory as test.c, but I really want it to function like <stdio.h> or any other default header.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
Trying to run assembly in C, but won't compile with gcc
main.c
#include <stdio.h>
#include "assem.s"
void extern testing(int * a);
int main(){
int b = 8;
int * a = &b;
testing(a);
printf("%d",*a);
}
assem.s
.globl testing
testing:
ret
gcc
gcc main.c assem.s -o test.exe
error
expected identifier or '(' before '.' token .globl testing
When you do #include "assem.s" it takes the contents of the file assem.s and places it into the C at that point. The result would have the compiler trying to compile this:
#include <stdio.h>
.globl testing
testing:
ret
void extern testing(int * a);
int main(){
int b = 8;
int * a = &b;
testing(a);
printf("%d",*a);
}
Of course that's not what you want. The compiler attempted to compile the line .globl testing and it failed because it isn't proper C syntax and results in the error you got. All you have to do is remove #include "assem.s".
assem.s will be assembled and then linked into the executable with the command gcc main.c assem.s -o test.exe. This one GCC command is the equivalent of the following except that intermediate object files won't be generated:
gcc -c main.c -o main.o
gcc -c assem.s -o assem.o
gcc main.o assem.o -o test.exe
You assemble/compile .s and .S files in the same fashion as .c files. .s and .S files should not be used with the C #include directive. There is a misconception among some new GCC users that they should be included and not assembled/linked separately.
You should change main.c like this. Then they could work.
#include <stdio.h>
//#include "assem.s"
void extern testing(int * a);
int main(){
int b = 8;
int * a = &b;
testing(a);
printf("%d",*a);
return 0;
}
I have a test.c file which contains main() function and some test cases and it cannot be modified it(such as adding "include *.h"). Then I have a foo.c file which contains some functions(no main() function). These functions will be tested through test cases in test.c file. What I'm going to do is use foo.c as a library and link it to test.c file. And here is the simple code.
test.c
//cannot modify
int main(){
...
bar();
...
}
foo.c
#include "foo.h" //I will explain this below.
int bar(){
...
}
I'm trying to implement an interface using .h file, such as
foo.h
#ifndef _FOO_H_
#define _FOO_H_
extern int bar();
#endif
Then using cmd line
gcc -c foo.c
gcc -o output test.c foo.o
./output
You may guess the result. There is a warning that "implicit declaration of function 'bar' is invalid in C99 [-Wimplicit-function-declaration]". And the test.c file cannot run correctly.
Could someone help me about this? Thank you so much!
Your problem is:
test.c has a call to bar() in it.
test.c doesn't have any declaration for bar, nor does it have an #include for a .h file that declares bar.
You are not allowed to change test.c in any way to add either a declaration or an #include.
This is a hard problem. The C language requires there be a prototype/declaration for bar in test.c! It can be written directly in the test.c file (write extern int bar(); before you call it), or the declaration can come in from another file with an #include statement, but you must have it.
Luckily, GCC has a way to force an #include statement into a file while it's compiling the file. You don't have to change test.c in order to make it start with #include "foo.h". This will solve your problem:
gcc -c -include foo.h test.c
You need to include the declaration of bar in the test.c file:
#include "foo.h"
So that the compiler have the prototype in the translation unit, of test.c.
I have this block of code. I have to move the given function display_name() into another .c file, compile it, and find the error that was caused due to the migration of the function and correct it by creating a header file with a prototype. How can I do it?
#include <stdio.h>
#include <string.h>
#include <ctype.h>
char student[]="Rasmus Lerdorf";
void display_name()
{
printf("Student Name : %s",student);
}
int main()
{
display_name();
}
these are the changes i made but again i still get an error in the main.cpp. it doesnt allow me to include the displayname.h file.
displayname.h
void display_name(void);
displayname.cpp
#include <stdio.h>
#include "displayname.h"
char student[] = "Rasmus Lerdorf";
void display_name()
{
printf("Student Name : %s", student);
}
main.cpp
#include <stdio.h>
#include "displayname.h"
int main()
{
display_name();
}
errors are:
3 IntelliSense: identifier "display_name" is undefined c:\Users\konstantinos\Desktop\main\main.cpp 7 2 Cproject
2 IntelliSense: cannot open source file "displayname.h" c:\Users\konstantinos\Desktop\main\main.cpp 2 1 Cproject
Error 1 error C1083: Cannot open include file: 'displayname.h': No such file or directory c:\users\konstantinos\desktop\main\main.cpp 2 1 Cproject
Prototype functions work like this: for each set of functions that you write (except main) you need a definition and an implementation. Definitions are usually stored in header files (extension .h) whereas implementations are stored in source files (extension .c).
Here is an example of how you could arrange your code to solve your problem.
Definition: display.h
// This file contains the definitions of the functions which you want to call from another file
void display_name(void);
Implementation: display.c
#include "display.h"
#include <stdio.h>
static char student[]="Rasmus Lerdorf";
void display_name()
{ printf("Student Name : %s",student);
}
With both the definition defined and the desired implemented, now you can call the function from your main source file.
Implementation: main.c
#include "display.h"
#include <stdio.h>
int main()
{
display_name();
}
This is how you link together a prototype of a function and the implementation of a function. You can expand this by adding more prototypes to display.h, implementing those prototyped functions in display.c, and then calling them throughout your code.
To build, both of these .c files must be included in your build phase. If you build from the command line, you need to do something like this (I'm assuming that your compiler is gcc):
cc display.c main.c -o program
Hope this helps.
Your header file, let's call it displayname.h should contain the declaration:
void display_name(void);
It's usually also best to create an include guard, which avoids causing problems if a header is included more than once:
#ifndef DISPLAYNAME_H
#define DISPLAYNAME_H
void display_name(void);
#endif /* DISPLAYNAME_H */
Then, in your displayname.c, you would include that header plus any others needed by the function, and define your constant and the function:
#include <stdio.h>
#include "displayname.h"
char student[]="Rasmus Lerdorf";
void display_name()
{
printf("Student Name : %s",student);
}
And in your main.c, you would also include that header:
#include "displayname.h"
int main()
{
display_name();
return 0;
}
I don't know what compiler you are using, but if you're on a Unix-like system (Linux, Mac OS X, or something like msys or Cygwin under Windows), you would compile and link them as follows (you can replace cc with your specific compiler, such as gcc or clang, though on most systems cc should exist and point to the default compiler for that system):
cc -c -o displayname.o displayname.c
cc -c -o main.o main.c
cc -o myprogram main.o displayname.o
You could also abbreviate this as:
cc -o myprogram main.c displayname.c
I also recommend, when you are learning, to use the -Wall -Wextra -Werror flags, to give you as many warnings as possible, and not allow compilation to proceed if there are any warnings. To make this more convenient, so you don't have to type the whole command every time, you can define a simple Makefile; the following uses GNU make syntax, if you don't have GNU make let me know and I'll edit it to use a more portable syntax:
CFLAGS=-Wall -Wextra -Werror
myprogram: main.o displayname.o
cc -o $# $^
%.o: %.c
cc -c $(CFLAGS) -o $# $<
main.o: displayname.h
displayname.o: displayname.h
If you have this set up, you can just type make and it will recompile everything that it needs to.
edit: I see now from your comments that you are using Visual Studio, so the above tips on how to compile and link using cc and make are not relevant to you. It has been too long since I have used Visual Studio to walk you through that myself, but Microsoft has a reasonable walkthrough of how to create and build a project that you can follow. The tutorial is for C++, but it should work similarly for C, just keep in mind that files should be named .c if they are written in C, and only .cpp if they are written in C++.
i did what you said! i created the 3 specific files displayname.h for the prototype , displayname.cpp in which the function display_name() stays and main.cpp in which i call the function display_name(). the problem again is that when i include the file displayname.h in the displayname.cpp it works fine, but when i include it in the main.cpp i have an underline error in the include. what is wrong?
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I know that this has been answered, but I still can't get it to work.
first.c
#include <stdio.h>
#include "second.h"
int main(void){
printf("%d\n", addone(5));
return 0;
}
second.c
int addone(int a){
return ++a;
}
second.h
int addone(int a);
When I run gcc -o executable first.c -Wall then it shows me undefined reference to addone
#include <stdio.h>
#include "second.h"
include those headers in second.c file too and then write Makefile and compile it with make.
Makefile:
CFLAGS = -g -O -Wall
OBJ = first.o second.o
# An explicit rule is required to link.
# Compilation is handled automatically.
first: $(OBJ)
$(CC) $(CFLAGS) -o first $(OBJ)
# Declare that both object files depend on the header file.
first.o second.o: second.h
# Conventionally 'make clean' removes what 'make' creates.
# Not strictly required.
.PHONY: clean
clean:
-rm -f first $(OBJ)
You can't just copy and paste the above; you must also change both indented lines (the ones with shell commands on them) so that the indentation is a single hard TAB character. If you don't do this you will get a cryptic error message:
Makefile:5: *** missing separator. Stop.
first.c:
#include <stdio.h>
#include "second.h"
int main(void){
printf("%d\n", addone(5));
return 0;
}
second.c
#include <stdio.h>
#include "second.h"
int addone(int a){
return ++a;
}
second.h
#include <stdio.h>
int addone(int a);