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;
}
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 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
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);
Starting to get my head around makefiles for my C programs, but having some trouble when trying to include multiple files. Ignoring the fact that the program below is incomplete (in terms of functionality but not compilation), I'm trying to get this program compiling and running using a make file.
Here is my make file:
main: main.o IntList.o
gcc -o main main.o IntList.o
main.o: main.c
gcc -c -ansi -pedantic -Wall main.c
IntList.o: IntList.c IntList.h
gcc -c -ansi -pedantic -Wall Intlist.c
And here is the error I am receiving:
gcc -c -ansi -pedantic -Wall Intlist.c
gcc -o main main.o IntList.o
ld: duplicate symbol _getNewInt in IntList.o and main.o
collect2: ld returned 1 exit status
make: *** [main] Error 1
The code for the program is below. I'm not sure whether it's the make file or my includes in the program files that are causing problems (or both!)
Any help would be great. Cheers.
Edit: Any tips to steer me in the right direction in terms of modularization would be much appreciated as I'm not sure if I am doing this the best way.
IntList.h
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Constants */
#define MAX_INTS 10
/* Signed ints can have a maximum of 10 digits. We make the length 11 to
* allow for the sign in negative numbers */
#define MAX_INPUT_LENGTH 11
#define EXTRA_SPACES 2
/* Typedefs / Structs */
typedef struct {
int list[MAX_INTS];
int noInts;
} IntList;
/* Proto Types */
int insertIntToList(int *list);
void shiftList(int offset);
void displayList();
IntList.c
#include "IntList.h"
int getNewInt(int *list)
{
int valid = 0, inputInt;
char inputString[MAX_INPUT_LENGTH + EXTRA_SPACES];
while(!valid)
{
printf("Input an int: ");
valid = 1;
if((fgets(inputString, MAX_INPUT_LENGTH + EXTRA_SPACES, stdin)) != NULL)
{
sscanf(inputString, "%d", &inputInt);
/* Check first that the input string is not too long */
if(inputString[strlen(inputString) - 1] != '\n')
{
printf("\nError: Too many characters entered \n");
valid = 0;
}
printf("\nThe Int: %d", inputInt);
printf("\n");
}
}
}
void shiftList(int offset)
{
}
void displayList()
{
}
main.c
#include <stdio.h>
#include <stdlib.h>
#include "IntList.c"
int main(void)
{
int intList[10];
getNewInt(intList);
return EXIT_SUCCESS;
}
Don't include the .c file in main, include the .h file. Otherwise the code in IntList.c gets compiled both into the IntList.o and the main.o, so you'll get duplicate symbols.
Use this in main.c instead of IntList.c:
#include "IntList.h"
#include "IntList.c"
should be:
#include "IntList.h"
Also (though nothing to do with your problem) I would recommend not using mixed case in the names of source files, as it can lead to portability problems and hard to diagnose "no such file" errors - use all lower case, like the standard library headers do.
Don't #include "IntList.c" into main.c
You should not have:
#include "IntList.c"
in your main program, it should be:
#include "IntList.h"
By including the C file, you create a getNewInt in both your main and IntList object files, which is why you're getting the duplicate definition error when you try to link them together.
main.c should include "IntList.h", not "IntList.c".
If you include IntList.c, the functions in IntList.c will be implemented both in IntList.o and in main.o, which would produce the "duplicate symbol" error you're seeing.
As others have mentioned, you include .h files, not .c files
Also when you compile, you only compile .c files so you should remove any references to .h files in your Makefile