Added library for shared functions.

This commit is contained in:
Warwick 2023-07-20 18:15:47 +01:00
parent efd17b030e
commit 09a1f69010
6 changed files with 47 additions and 21 deletions

4
.gitignore vendored
View file

@ -5,4 +5,6 @@ cmake_install.cmake
compile_commands.json compile_commands.json
.dir-locals.el .dir-locals.el
Makefile Makefile
Urchin-* urchin-*
lib*.a
lib*.so

View file

@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.16) cmake_minimum_required(VERSION 3.16)
project(Urchin project(urchin
VERSION 0 VERSION 0
DESCRIPTION "Simple Mud Experiment in C" DESCRIPTION "Simple Mud Experiment in C"
HOMEPAGE_URL "https://git.warwick-new.co.uk/" HOMEPAGE_URL "https://git.warwick-new.co.uk/"
@ -9,6 +9,15 @@ set(CMAKE_C_STANDARD 90)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON) set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_C_CLANG_TIDY) set(CMAKE_C_CLANG_TIDY)
# Compile Shared Library
file(GLOB_RECURSE LIBRARY_SOURCE_FILES
${CMAKE_SOURCE_DIR}/src/shared_lib/*.c)
file(GLOB_RECURSE LIBRARY_HEADER_FILES
${CMAKE_SOURCE_DIR}/src/shared_lib/*.h)
add_library(${PROJECT_NAME} SHARED ${LIBRARY_HEADER_FILES} ${LIBRARY_SOURCE_FILES})
# Compile Server # Compile Server
file(GLOB_RECURSE SERVER_SOURCE_FILES file(GLOB_RECURSE SERVER_SOURCE_FILES
${CMAKE_SOURCE_DIR}/src/server/*.c) ${CMAKE_SOURCE_DIR}/src/server/*.c)
@ -17,6 +26,7 @@ file(GLOB_RECURSE SERVER_HEADER_FILES
${CMAKE_SOURCE_DIR}/src/server/*.h) ${CMAKE_SOURCE_DIR}/src/server/*.h)
add_executable(${PROJECT_NAME}-server ${SERVER_HEADER_FILES} ${SERVER_SOURCE_FILES}) add_executable(${PROJECT_NAME}-server ${SERVER_HEADER_FILES} ${SERVER_SOURCE_FILES})
target_link_libraries(${PROJECT_NAME}-server ${PROJECT_NAME})
# Compile Client # Compile Client
file(GLOB_RECURSE CLIENT_SOURCE_FILES file(GLOB_RECURSE CLIENT_SOURCE_FILES
@ -26,3 +36,4 @@ file(GLOB_RECURSE CLIENT_HEADER_FILES
${CMAKE_SOURCE_DIR}/src/client/*.h) ${CMAKE_SOURCE_DIR}/src/client/*.h)
add_executable(${PROJECT_NAME}-client ${CLIENT_HEADER_FILES} ${CLIENT_SOURCE_FILES}) add_executable(${PROJECT_NAME}-client ${CLIENT_HEADER_FILES} ${CLIENT_SOURCE_FILES})
target_link_libraries(${PROJECT_NAME}-client ${PROJECT_NAME})

View file

@ -1,6 +1,7 @@
// Sorry in advance for all the comments. This project is also a way for me to // Sorry in advance for all the comments. This project is also a way for me to
// learn C. // learn C.
#include "error.h"
#include <netdb.h> // Contains host structure #include <netdb.h> // Contains host structure
#include <netinet/in.h> //sockaddr_in #include <netinet/in.h> //sockaddr_in
#include <stdio.h> #include <stdio.h>
@ -10,15 +11,15 @@
#include <sys/socket.h> // sockaddr #include <sys/socket.h> // sockaddr
#include <sys/types.h> #include <sys/types.h>
void error_crash(const char *msg) { // void error_crash(const char *msg) {
fprintf(stderr, "error crash: %s\n", msg); // fprintf(stderr, "error crash: %s\n", msg);
exit(EXIT_FAILURE); // exit(EXIT_FAILURE);
} // }
//
void error_warn(const char *msg) { fprintf(stderr, "warning: %s\n", msg); } // void error_warn(const char *msg) { fprintf(stderr, "warning: %s\n", msg); }
//
// TODO: Save the logs somewhere // // TODO: Save the logs somewhere
void error_log(const char *msg) { fprintf(stderr, "log: %s\n", msg); } // void error_log(const char *msg) { fprintf(stderr, "log: %s\n", msg); }
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
// Set up containers for file descriptor id's // Set up containers for file descriptor id's

View file

@ -1,6 +1,7 @@
// Sorry in advance for all the comments. This project is also a way for me to // Sorry in advance for all the comments. This project is also a way for me to
// learn C. // learn C.
#include "error.h"
#include <netinet/in.h> //sockaddr_in #include <netinet/in.h> //sockaddr_in
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> // Keeping for conversion functions #include <stdlib.h> // Keeping for conversion functions
@ -9,16 +10,6 @@
#include <sys/socket.h> // sockaddr #include <sys/socket.h> // sockaddr
#include <sys/types.h> #include <sys/types.h>
void error_crash(const char *msg) {
fprintf(stderr, "error crash: %s\n", msg);
exit(EXIT_FAILURE);
}
void error_warn(const char *msg) { fprintf(stderr, "warning: %s\n", msg); }
// TODO: Save the logs somewhere
void error_log(const char *msg) { fprintf(stderr, "log: %s\n", msg); }
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
// Set up containers for file descriptor id's // Set up containers for file descriptor id's
int sockfd, clientsockfd, portno, errflag; int sockfd, clientsockfd, portno, errflag;

11
src/shared_lib/error.c Normal file
View file

@ -0,0 +1,11 @@
#include "error.h"
void error_crash(const char *msg) {
fprintf(stderr, "error crash: %s\n", msg);
exit(EXIT_FAILURE);
}
void error_warn(const char *msg) { fprintf(stderr, "warning: %s\n", msg); }
// TODO: Save the logs somewhere
void error_log(const char *msg) { fprintf(stderr, "log: %s\n", msg); }

10
src/shared_lib/error.h Normal file
View file

@ -0,0 +1,10 @@
#pragma once
#include <stdio.h>
#include <stdlib.h>
void error_crash(const char *msg);
void error_warn(const char *msg);
void error_log(const char *msg);