Skip to content

Commit

Permalink
Update C code generation and compilation.
Browse files Browse the repository at this point in the history
  • Loading branch information
MilanSkocic committed Nov 20, 2024
1 parent b4609bc commit d3c9f96
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 15 deletions.
4 changes: 2 additions & 2 deletions C/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

.PHONY: headers sources build clean

all: headers sources
all: headers sources build


headers:
Expand All @@ -14,7 +14,7 @@ sources: headers

build:
mkdir -p $(BUILD_DIR)
make -C src build
make -C src

clean:
make -C include clean
Expand Down
6 changes: 6 additions & 0 deletions C/example/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@


all: example

example: example.c
gcc -I../include -L../build/ -lcodata $(CFLAGS_RELEASE) $(FPM_CFLAGS) $< -o $@
11 changes: 6 additions & 5 deletions C/example/example.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#include <stdio.h>
#include "codata.h"
#include "codata_version.h"
#include "codata_constants_2022.h"

int main(void){

printf("########## EXAMPLE IN C ##########\n");

printf("%s\n","########## VERSION ##########");
printf("version = %s\n", codata_get_version());
printf("version = %s\n", version);

printf("%s\n","########## CONSTANTS ##########");
printf("c = %f\n", SPEED_OF_LIGHT_IN_VACUUM.value);
Expand All @@ -16,9 +17,9 @@ int main(void){

printf("%s\n","########## OLDER VALUES ##########");
printf("Mu_2022(latest) = %23.16f\n", MOLAR_MASS_CONSTANT.value);
printf("Mu_2018 = %23.16f\n", MOLAR_MASS_CONSTANT_2018.value);
printf("Mu_2014 = %23.16f\n", MOLAR_MASS_CONSTANT_2014.value);
printf("Mu_2010 = %23.16f\n", MOLAR_MASS_CONSTANT_2010.value);
//printf("Mu_2018 = %23.16f\n", MOLAR_MASS_CONSTANT_2018.value);
//printf("Mu_2014 = %23.16f\n", MOLAR_MASS_CONSTANT_2014.value);
//printf("Mu_2010 = %23.16f\n", MOLAR_MASS_CONSTANT_2010.value);

return 0;
}
Binary file added C/example/example.exe
Binary file not shown.
1 change: 1 addition & 0 deletions C/include/codata.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#else
#define ADD_IMPORT
#endif
#include "codata_version.h"
#include "codata_constants_type.h"
#include "codata_constants_2010.h"
#include "codata_constants_2014.h"
Expand Down
3 changes: 2 additions & 1 deletion C/include/codata_constants_type.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#ifndef CODATA_CONSTANTS_TYPE_H
#define CODATA_CONSTANTS_TYPE_H

struct codata_constant_type{
struct codata_constant_type
{
char name[65];
double value;
double uncertainty;
Expand Down
2 changes: 1 addition & 1 deletion C/include/codata_version.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#ifndef CODATA_VERSION_H
#define CODATA_VERSION_H
const char version[32] = 2.0.0
const char version[32] = "2.0.0";
#endif
19 changes: 15 additions & 4 deletions C/src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ CBUILD_DIR = ../build
AST_SRC=$(wildcard $(AST_DIR)/*.toml)
C_SRC = codata_constants_2010.c codata_constants_2014.c codata_constants_2018.c codata_constants_2022.c
C_OBJ=$(patsubst %.c, $(CBUILD_DIR)/%.o, $(C_SRC))
DLIB=$(CBUILD_DIR)/$(LIBNAME)$(EXT)
DLIB=$(CBUILD_DIR)/$(LIBNAME).a

.PHONY: clean

all: sources
all: sources build shared

sources: $(C_SRC)

Expand All @@ -32,8 +32,19 @@ codata_constants_2022.c: $(AST_DIR)/codata_2022.toml
$(CBUILD_DIR)/%.o: %.c
gcc -c -I ../include $(CFLAGS_RELEASE) $(FPM_CFLAGS) $< -o $@

$(CBUILD_DIR)/$(LIBNAME)$(EXT): $(C_OBJ)
gcc -shared $(C_OBJ) -o $@
$(CBUILD_DIR)/$(LIBNAME).a: $(C_OBJ)
ar -rs $@ $^

shared: build shared_$(PLATFORM)

shared_linux:
$(CC) -shared -o $(CBUILD_DIR)/$(LIBNAME).so -Wl,--whole-archive $(CBUILD_DIR)/$(LIBNAME).a -Wl,--no-whole-archive

shared_darwin:
$(CC) -dynamiclib -install_name @rpath/$(LIBNAME).dylib $(FPM_LDFLAGS) -o $(CBUILD_DIR)/$(LIBNAME).dylib -Wl,-all_load $(CBUILD_DIR)/$(LIBNAME).a

shared_windows:
$(CC) -shared $(FPM_LDFLAGS) -o $(CBUILD_DIR)/$(LIBNAME).dll -Wl,--out-implib=$(CBUILD_DIR)/$(LIBNAME).dll.a,--export-all-symbols,--enable-auto-import,--whole-archive $(CBUILD_DIR)/$(LIBNAME).a -Wl,--no-whole-archive

clean:
rm -rf $(C_SRC)
Expand Down
4 changes: 2 additions & 2 deletions configure.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ INCLUDE_DIR="./include"
FPM_FFLAGS="-std=f2008 -pedantic -Wall -Wextra"
FPM_CFLAGS="-std=c11 -pedantic -Wall -Wextra"
FPM_LDFLAGS=""
CFLAGS_RELEASE="-fPIC -O3 -funroll-loops"
CFLAGS_DEBUG="-fPIC -g"
CFLAGS_RELEASE="-fPIC -O3 -funroll-loops -fmax-errors=1"
CFLAGS_DEBUG="-fPIC -g -fmax-errors=1"
DEFAULT_INSTALL_DIR="$HOME/.local"
PLATFORM="linux"
EXT=".so"
Expand Down

0 comments on commit d3c9f96

Please sign in to comment.