I'm trying to access the programs array in my main file. It is declared in the header file and initialized in a separate module called fileReader. The error message I receive is
Undefined symbols for architecture x86_64:
"_programs", referenced from:
_main in test-0bf1e8.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "header.h"
#include "fileReader.c"
int main() {
readPrograms();
for (int i=0; i<4; i++) {
printf("%s", programs[i]);
}
return 0;
}
fileReader.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "header.h"
int readPrograms() {
int i=0;
int numProgs=0;
char* programs[50];
char line[50];
FILE *file;
file = fopen("files.txt", "r");
while(fgets(line, sizeof(line), file)!=NULL) {
//add each filename into array of programs
programs[i]=strdup(line);
i++;
}
fclose(file);
return 0;
}
header.h
extern char* programs[];
Thanks in advance
You are not supposed to include C files from other C files, only the header files.
Here is what you need to fix:
Add a prototype of readPrograms function to the header.h
Remove #include "fileReader.c" from the main.c file
Add a definition of programs array to one of your C files (say, main.c).
Remove declaration of the local programs from readPrograms
The definition of programs that you put in main.c should look like this:
char* programs[50];
You can put it before or after your main() function.
Related
I creating a program in c language and i using the Visual Studio Code for the first time, my functions in the header files don't function. This is my code in main:
#include <stdio.h>
#include "PilhaDinamica.h"
#include "PilhaEstatica.h"
int main()
{
Pilha *p = criaPilha();
return 0;
}
And this is my .h file:
#ifndef PILHADINAMICA_H_INCLUDED
#define PILHADINAMICA_H_INCLUDED
typedef struct Nodo{
char info;
struct Nodo*prox;
} nodo;
typedef struct {
nodo * Topo;
} Pilha;
Pilha * criaPilha();
int pilha_vazia(Pilha *p);
void push(Pilha *p, char times);
char pop(Pilha *p);
#endif
This is my file with the functions:
#include "PilhaDinamica.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Pilha *criaPilha()
{
Pilha *p = (Pilha*) malloc(sizeof(Pilha));
p->Topo = NULL;
return p;
}
And this is shown in my output: "...\AppData\Local\Temp\ccmjk1nS.o:main.c:(.text+0xf): undefined reference to `criaPilha'
collect2.exe: error: ld returned 1 exit status"
what can i do to make it compile correctly?
As a general rule of thumb, header files (*.h) contains declarations (type, variable and function declarations) and source files (*.c) the definitions of those declarations.
At the compilation step, only source files will be compiled (because the definitions are there). A program or library creation is a 2 (actually more, like preprocessing and more but for simplicity we keep it at 2) step process:
creating object files
e.g. gcc -c -o object_file_name.o source_file_name.c
link those object files into an executable or static/shared library
e.g. gcc -o program_or_library_name object_file_1.o object_file_2.o ...
So, in your case you have to call the compiler two times for your source files (with the -c flag) and once to link those created object files into an executable.
Note: If you're using a different compiler other than gcc, have a look at the documentation on how to create object files and link them together.
In a CLion project, I have two C-language source files, "main.c" and "list.c".
The source file "main.c" has this:
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
The source file "list.c" has this:
#include <stdio.h>
int printFoo() {
printf("I want Krabby Patties!\n");
return 0;
}
Now how do I call printFoo() from the main() function? I know I cannot do an include<list.c> in main.c since that will cause a multiple definitions error.
CLion uses CMake for organizing and building project.
CMakeLists.txt contains instructions for building.
Command add_executable(program main.c list.c) creates executable with files main.c and list.c. Add all source files to it. You can add headers, but it isn't necessary.
Header files contain definitions of functions and other things, source files for realization, but you can merge them.
main.c
#include "list.h"
int main() {
printFoo();
return 0;
}
list.h
#pragma once
int printFoo();
list.c
#include "list.h"
#include <stdio.h>
int printFoo(){
return printf("I want Krabby Patties!\n");
}
#pragma once tels compiler to include header file once. If you have more than one include of one file without #pragma once, you'll catch an error.
You can create one header file "list.h"
#ifndef __LIST_H__
#define __LIST_H__
int printFoo();
#endif
Then include it in main.c:
#include <stdio.h>
#include "list.h"
int main() {
printf("Hello, World!\n");
printFoo();
return 0;
}
I have 3 .c files main.c, fun1.c, fun2.c
char buff[50];//in fun1.c
char *arg; //in fun2.c
arg = strstr(buff, "001"); //in fun2.c
I want to print buff in fun2.c but it gives an error buff undeclared, even though I declared it in fun1.h as extern char buff[];
There are functions in fun1.c and fun2.c each
It is hard to say what is wrong with your particular program, but here is an example which links 2 .c files with one .h file.
1. A header file functions.h:
#include <stdio.h>
extern void func();
Where I use extern to provide definitions for another file.
2. Now, a functions.c file which uses this header file:
#include "functions.h"
void func() {
printf("hello");
}
This needs to #include the header file, and use the function void() to print a message.
3. Finally, a main.c file which links it all together:
#include <stdio.h>
#include <stdlib.h>
#include "functions.h"
int main(void) {
func();
return 0;
}
Which also needs function.h as it uses func(). You then can compile the code as:
gcc -Wall -Wextra -g main.c functions.c -o main
You could also look into makefiles, which would reduce this long compilation line to simply make.
I read that the C file containing the function definition should be of the same name as the header file. So, i created two files: functions.h, functions.c & lastly the main.c file which calls the functions which are defined inside of the functions.c file.
//functions.h file
void check();
I have declared check function in the header file
//functions.c file
#include <stdio.h>
#include "functions.h"
int main(void){
void check(){
printf("\nThis is a Test\n");
}
return 0;
}
This file contains all the function definition. But one thing i want to clear out is, I saw some another question on stackoverflow of basically the same type but in function file he had just included the header files and function definitions, without main(). Shouldn't that .c file throw an error?
//main.c file
#include "function.h"
#include <stdio.h>
int main(void)
{
check();
return 0;
}
when i open terminal and type the command to compile the code:
clang main.c
I get an error:
Undefined symbols for architecture x86_64:
"_check", referenced from:
_main in heap-22db64.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
May be i haven't compiled functions.c file that's the reason i see this. I am just compiling main.c. I am not sure about this linking error. If i have 35 .c files. Compiling all of them via commandline would be harder task? What should be my approch to deal with these big projects. Having multiple C & header files?
Here's the typical scenario:
// functions.c
void check(void) {
// do stuff
}
Note: just the definition of check, and nothing else. Then a header:
// functions.h
extern void check(void);
Just a declaration. Then the main file:
// main.c
#include "functions.h"
int main(int argc, char *argv[]) {
check();
}
When definitions are provided in another file, you have to specify that like so using the extern keyword:
functions.h:
extern void check();
functions.c:
void check()
{
printf("\nThis is a Test\n");
}
For my question let's suppose I have two functions, both of them with the prototypes on a .h file in a library folder, and the implementation in a .c auxiliary file (shown below), and I will use both of them in my program.
calsis.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include "include/calsis.h" /* Extern header */
char folder_name[30] = "Information";
void no_args() /* Function 1 */
{
printf("Hello, world!\n");
if ( mkdir(folder_name, S_IRWXU) == -1 )
perror("Can't create a new folder");
}
void with_args(char *foo) /* Function 2 */
{
printf("Hello, world!\n");
printf("Name: %s\n", foo);
if ( mkdir(folder_name, S_IRWXU) == -1 )
perror("Can't create a new folder");
}
For something I will do later, I need in both functions to create a folder with mkdir, but, in the generation of the object file calsis.o by the compilation of the .c file with the implemented functions, the compilation with GCC gives me a warning that the mkdir function is implicity declared.
Any idea I can remove this warning?
You haven't included the header for mkdir:
From man(2) mkdir:
SYNOPSIS
#include <sys/stat.h>
#include <sys/types.h>
int mkdir(const char *pathname, mode_t mode);