Using C function in another function [closed] - c

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);

Related

Compile time error while running multiple c files

#Edited
I am tested on simple two files now that are add.h and add.c.
I made a Makefile in order to compile my program. Here is my makefile.
# Make file for running the project
CC=gcc
CFLAGS= -Wall -g
LDFLAGS = -include
OBJFILES = add.o
LIB = add.h
TARGET = add
all: ${TARGET}
%.o: %.c
${CC} ${CFLAGS} -c -o $# $<
${TARGET}: ${OBJFILES}
${CC} ${CFLAGS} -o ${TARGET} ${OBJFILES}
clean:
rm -f $(OBJFILES) $(TARGET) *~
when I run
make add
I get the following error:
gcc -Wall -g -c -o add.o add.c
gcc -Wall -g -o add add.o
Undefined symbols for architecture x86_64:
"_b", referenced from:
_main in add.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [add] Error 1
here are snippets of my code
add.c
#include <stdio.h>
#include <stdlib.h>
#include "add.h"
int main(void) {
int a = 10, b=20;
add(a, b);
return 0;
}
add.h
#ifndef __ADD_H_
#define __ADD_H_
extern int a,b;
int add(a,b)
{
return a+b;
}
#endif // __ADD_H_
You are lacking a rule to actually build your object files. Right now you only have one to link them all together once they already exist. Try adding:
%.o: %.c
${CC} ${CFLAGS} -c -o $# $<
This tells make how to build object files out of source files.
You are misusing the extern keyword, losing track of your variables and making your code too complicated.
Let's try something very simple:
int main(void) {
int a = 10; b=20;
return 0;
}
This fails. The compiler complains about the statement b=20;, since it has never heard of this b. The semicolon that made this a separate statement was either a typo or a conceptual error caused by declaring extern int b elsewhere. There is no need for extern here, at least not yet.
This:
int main(void) {
int a=10, b=20;
return 0;
}
works.
Now for an add function.
int add(int a, int b)
{
return a+b;
}
int main(void) {
int a = 10, b=20;
add(a,b);
return 0;
}
Note that the a and b in add are not the same variables as the a and b in main. This is crucial; do not proceed until you understand it.
Now add a declaration of the add function:
int add(int a, int b); // <- declaration
int add(int a, int b) // <- definition
{
return a+b;
}
The declaration can be moved into a header file (add.h); the definition belongs in a source file (add.c).
Finally, I would advise you to add a line to the makefile:
add.o: add.h
Aren't you missing an #endif at the end of the last header file queue.h ?
This can be for sure an issue when compiling. Moreover, aren't you missing the main_application.h header file you are then including within the stack.h one for example?
Have a look at this: https://medium.com/#m.muizzsuddin_25037/error-ld-symbol-not-found-for-architecture-x86-64-a5e5b648ffc seems helpful here and point our attention to the header files again!..

Calling Assembly from C compile error gcc [closed]

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;
}

Ubuntu Message Queue Makefile Error

I have a problem with message queues in C in Ubuntu. I use VirtualBox to run the Ubuntu.
I took error which is "undefined reference to mq_open. ld returned 1 exit status".
I know there is a same question as this but I tried that solution but it did not worked, so I want to ask again. Please help!
Here is my code, it is really simple but I can not even compile it.
this is my deneme.c
#include <stdlib.h>
#include <mqueue.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include "sharedMsg.h"
int main()
{
mqd_t mq;
mq = mq_open(MQNAME, O_RDWR | O_CREAT, 0666, NULL);
}
this is my sharedMsg.h
struct item{
char *word;
int lineNum;
};
#define MQNAME "/sentmsg"
and finally this is my MakeFile
all: deneme
deneme: deneme.c
gcc -g -Wall -o deneme deneme.c -lrt
clean:
rm -fr *~ *.o
The problem with your Makefile is that your all target depends on demene but the target that specifies -lrt is called demene1. Thus, the default inference rules kick in and demene is attempted to be linked without -lrt. The rule for deneme1 is also wrong in that it attempts to create a binary named deneme, even though the rule should create deneme1. To resolve this, change the first to third lines to
all: deneme1
deneme1: deneme.c
gcc -g -Wall -o deneme1 deneme.c -lrt
or the second line to
deneme: deneme.c
(Note: The OP has edited the question and fixed the Makefile after this answer)
You have a broken Makefile.
The reason why you've even seen the linker error is that you have probably executed make deneme. In that case make will try to compile dename.c with default compiler options (because it did not find a target called deneme) and the default options do not include -lrt.
You have to fix your Makefile (replace deneme1: with deneme:)
all: deneme
deneme: deneme.c
gcc -g -Wall -o deneme deneme.c -lrt
clean:
rm -f deneme *~ *.o

Using header files in C [closed]

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

C language prototype creation

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?

Resources