Implicit declaration of mkdir - c

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

Related

Unable to retrieve a value inside a c header file

I have a header file where a few functions needs to be implemented in the header file and included in a main.c file to be tested. These are some library functions of String and encoding.
Once these methods are implemented in the header file I should be able to include this file in another c file and execute these methods.
#ifndef ABSTRING_H
#define ABSTRING_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#define ABSBLOCK 4096
typedef struct _abstring
{
unsigned char* val;
size_t length;
size_t space;
}absval;
abstring absval;
//Initialize string
abstring* initAbs() {
printf("%s", abval.val);
printf("%zu", abval.val);
abval.length = sizeof(abval.val);
abval.space = ABSBLOCK - sizeof(abval.val);
return &abval;
}
------------------ End of the header file (abString.h ) ------------------------
main.c file
#include "abString.h"
int main()
{
abstring absinit;
absinit.val = "abString";
printf("ABSBLOCK block size : %d .\n", ABSBLOCK);
initAbs();
return 0;
}
The issue I'm having is once I define a val in the main c file I'm not able to retrieve that value inside my header file in order to initialize the length and space.

Error with file pointer not defined

I have a file a.c, a.h which acts as a supporting module with functions from other files.
In those functions I have to open a file and do functions like reading data, writing data, closing it.
For these purposes I gave a global declaration for file pointer in a.c by
static FILE* pFile;
and used pFile directly.
The error that the compiler was throwing error is like :
"pFile" not defined in the function
What's wrong here?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <arpa/inet.h>
#define path "/tmp/diag.log"
FILE* pFile;
void a(bool value) {
if (value) {
pFile = fopen (path,"a");
return;
}
else if (!value) {
fclose (pFile);
}
}
void b(bool value) {
if(value)
{
fprintf (pFile,"%s",message);
}
}
Update:
The error was my makefile was taking the files from build directory and the changes were not getting reflected.Thanks a lot for your help
If you try to use this static FILE* pFile from another compilation units remove the static
in other file add:
extern FILE *pfile;

How to use a global array in multiple modules

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.

How to move C functions into separate file?

I'm getting started with C programming. I currently have a large file that contains a lot of functions. I would like to move these functions to a separate file so that the code is easier to read. However, I can't seem to figure out how to properly include/compile and can't find an example in any online tutorials that I've found. Here's a simplified example:
#include <stdlib.h>
#include <stdio.h>
void func1(void) {
printf("Function 1!\n");
}
void func2(void) {
printf("Function 2!\n");
}
int main(void) {
func1();
func2();
return 0;
}
How do you move C functions into a separate file? FYI: I'm using gcc.
Update: These answers are very helpful, thank you. Now it seems that my simplified example is not good enough because I realized the reason my program failed to compile is because I'm using a global variable in my functions.
#include <stdlib.h>
#include <stdio.h>
int counter = 0;
void func1(void) {
printf("Function 1!\n");
counter++;
}
int main(void) {
func1();
return 0;
}
Moving these functions to an external file doesn't work because they need to reference this global variable:
#include <stdlib.h>
#include <stdio.h>
#include "functions.c"
int counter = 0;
int main(void) {
func1();
counter = 100;
return 0;
}
How can I get around this issue?
Okay. Here we go.
Your main.c file
#include <stdlib.h>
#include <stdio.h>
#include "functions.h"
int main(void) {
func1();
func2();
return 0;
}
Your functions.h file
void func1(void);
void func2(void);
Your functions.c file
#include "functions.h"
void func1(void) {
printf("Function 1!\n");
}
void func2(void) {
printf("Function 2!\n");
}
Compile it with:
gcc -o main.exe main.c functions.c
The most common way is to place your function prototypes in a header file and your function implementations in a source file. For example:
func1.h
#ifndef MY_FUNC1_H
#define MY_FUNC1_H
#include <stdio.h>
// declares a variable
extern int var1;
// declares a function
void func1(void);
#endif
func1.c
#include "func1.h"
// defines a variable
int var1 = 512;
// defines a function
void func1(void) {
printf("Function 1!\n");
}
func2.h:
#ifndef MY_FUNC2_H
#define MY_FUNC2_H
#include <stdio.h>
void func2(void);
#endif
func2.c:
#include "func1.h" // included in order to use var1
#include "func2.h"
void func2(void) {
printf("Function 2 with var1 == %i\n", var1);
}
main.c:
#include <stdio.h>
#include "func1.h"
#include "func2.h"
int main(void) {
var1 += 512;
func1();
func2();
return 0;
}
You would then compile using the following:
gcc -c -o func1.o func1.c
gcc -c -o func2.o func2.c
gcc -c -o main.o main.c
gcc -o myprog main.o func1.o func2.o
./myprog
I only placed one function in each source/header pair for illustration. You could create just one header which includes the prototypes for all of the source files, or you could create multiple header files for each source file. The key is that any source file which will call the function, needs to include a header file which includes the function's prototype.
As a general rule, you only want a header file included once, this is the purpose of the #ifndef #define #endif macros in the header files.
First you have to learn the difference between a declaration and definition. A declaration tells the compiler that something, like a function, exists. A definition is, for the case of functions, the actual function implementation.
So what you do is move the definition to another file, but add a declaration in the file where the function is to be called. You then build both files together, and the compiler and linker will take care of the rest.
You can do something like this.
/* func1.c */
void func1(void) {
printf("Function 1!\n");
}
/* func2.c */
void func2(void) {
printf("Function 2!\n");
}
/* main.c */
#include "func1.c"
#include "func2.c"
int main ( void )
{
func1();
func2();
return 0;
}

Undefined Reference? But I've already implemented the function

display.h
#ifndef PRO_DISPLAY_H
#define PRO_DISPLAY_H
/** Initializes the display **/
int pro_display_init(void);
#endif /* PRO_DISPLAY_H */
display.c
#include "main.h"
static int height_ = 300;
static int width_ = 300;
static int bpp_ = 16;
static SDL_Surface* screen_ = NULL;
int pro_display_init(void)
{
screen_ = SDL_SetVideoMode(width_, height_, bpp_, SDL_HWSURFACE|SDL_DOUBLEBUF);
if (!screen_)
{
pro_sdl_error("Video initialization failed.");
return 0;
}
return 1;
}
main.h
#ifndef PRO_MAIN_H
#define PRO_MAIN_H
// standard headers
#include <stdlib.h>
#include <stdlib.h>
// conditional headers
#if defined(WIN32) || defined(_WIN32)
#include <windows.h>
#endif
// our own headers
#include "scripter.h"
#include "ttf_util.h"
#include "events.h"
#include "display.h"
// some macros
#define pro_error(...) fprintf(stderr, __VA_ARGS__)
#define pro_sdl_error(x) fprintf(stderr, "%s. \n=> %s\n", x, SDL_GetError())
#define pro_ttf_error(x) fprintf(stderr, "%s. \n=> %s\n", x, TTF_GetError())
#endif /* PRO_MAIN_H */
** main.c**
#include "main.h"
int main(int argc, char* argv[])
{
pro_display_init();
return 0;
}
The Error:
main.c|5|undefined reference to `pro_display_init()'|
Checked the build process. Made sure I was adding "display.c" to gcc's input files. I'm at my wit's end. Why the error?
display.c and main.c are compiled into their own "translation unit". What happens is that when trying to resolve symbols name (i.e. looking for pro_display_init), the C compiler thinks it's compiling a standalone .c unit. The proper way to go is to compile them separately and then link them, e.g.
gcc -c display.c # creates display.o
gcc main.c display.o # compiles main.o and then link with display.o
Of course, you'll be creating/reusing a Makefile soon that lets you define rules for all this.
I think, #include "main.h" or #include "display.h" (in main.h) "finds" the wrong include file. Check you include_path variable.

Resources