Reputation: 4646
For some reason the program is giving me this error
undefined reference to createNetworkDriver
Right now my folder structure is:
mini-kernel/
kernel/
drivers/
network/
network.h
network.c
util/
errors/
errors.c
main.c
network.h:
#ifndef _network_h_
#define _network_h_
#pragma once
#include <stdbool.h>
#include<stdint.h>
typedef struct
{
char *name;
bool initalized;
int32_t status;
int (*initalize)(void);
}NetworkDriver;
NetworkDriver* createNetworkDriver(void);
NetworkDriver* destroyNetworkDriver(NetworkDriver *self);
#endif
network.c:
#include "network.h"
#include "../../../util/errors/errors.c"
extern void die(const char *message);
NetworkDriver* createNetworkDriver(void)
{
printf("Working");
NetworkDriver nd = malloc(sizeof(NetworkDriver));
}
NetworkDriver* destroyNetworkDriver(NetworkDriver *self)
{
if(self->initalized == true)
free(self);
else
die("NET_DRIVER FAILED TO DESTROY");
}
errors.c:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
void die(const char *message)
{
if(errno) {
perror(message);
} else {
printf("ERROR: %s\n", message);
}
exit(1);
}
And finally my main.c:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include "kernel/drivers/network/network.h"
int main(int argc, char *argv[])
{
NetworkDriver *nd = createNetworkDriver();
}
And for some reason when i run the main.c
file gcc main.c -std=c99 -w -lm
, i get the error: undefined reference to createNetworkDriver
.
Im currently programming this online here.
Upvotes: 1
Views: 2514
Reputation: 409136
You need to build with all source files. Just including a header file is not enough for the compiler and linker to know which files you really need.
So do something like this instead:
$ gcc main.c util/errors/errors.c kernel/drivers/network/network.c -std=c99 -o my_kernel -w -lm
This will build each source file into a (temporary) object file and link all the object files together into a single executable file named my_kernel
(specified using the -o
option).
Upvotes: 3