1

I am completely new to makefiles and c compilation in general so I only know the very basic, so I decided to download a makefile generator extension for VSCode. How do I edit the file so that it compiles to a static library and does not give me an exception that a main function is missing.

This is the auto generated makefile:

########################################################################
####################### Makefile Template ##############################
########################################################################

# Compiler settings - Can be customized.
CC = gcc
CXXFLAGS = -std=c99 -Wall -pedantic
LDFLAGS = 

# Makefile settings - Can be customized.
APPNAME = myapp
EXT = .c
SRCDIR = src
OBJDIR = obj

############## Do not change anything from here downwards! #############
SRC = $(wildcard $(SRCDIR)/*$(EXT))
OBJ = $(SRC:$(SRCDIR)/%$(EXT)=$(OBJDIR)/%.o)
DEP = $(OBJ:$(OBJDIR)/%.o=%.d)
# UNIX-based OS variables & settings
RM = rm
DELOBJ = $(OBJ)
# Windows OS variables & settings
DEL = del
EXE = .exe
WDELOBJ = $(SRC:$(SRCDIR)/%$(EXT)=$(OBJDIR)\\%.o)

########################################################################
####################### Targets beginning here #########################
########################################################################

all: $(APPNAME)

# Builds the app
$(APPNAME): $(OBJ)
    $(CC) $(CXXFLAGS) -o $@ $^ $(LDFLAGS)

# Creates the dependecy rules
%.d: $(SRCDIR)/%$(EXT)
    @$(CPP) $(CFLAGS) $< -MM -MT $(@:%.d=$(OBJDIR)/%.o) >$@

# Includes all .h files
-include $(DEP)

# Building rule for .o files and its .c/.cpp in combination with all .h
$(OBJDIR)/%.o: $(SRCDIR)/%$(EXT)
    $(CC) $(CXXFLAGS) -o $@ -c $<

################### Cleaning rules for Unix-based OS ###################
# Cleans complete project
.PHONY: clean
clean:
    $(RM) $(DELOBJ) $(DEP) $(APPNAME)

# Cleans only all files with the extension .d
.PHONY: cleandep
cleandep:
    $(RM) $(DEP)

#################### Cleaning rules for Windows OS #####################
# Cleans complete project
.PHONY: cleanw
cleanw:
    $(DEL) $(WDELOBJ) $(DEP) $(APPNAME)$(EXE)

# Cleans only all files with the extension .d
.PHONY: cleandepw
cleandepw:
    $(DEL) $(DEP)
  • What kind of research have you made about (GCC) static libraries? – Some programmer dude Aug 12 '22 at 17:45
  • @Someprogrammerdude I've read https://dev.to/iamkhalil42/all-you-need-to-know-about-c-static-libraries-1o0b https://stackoverflow.com/questions/2734719/how-to-compile-a-static-library-in-linux and https://stackoverflow.com/questions/31421616/c-creating-static-library-and-linking-using-a-makefile but the existing makefile I have went waaaay over my head so I did not really know how to link the .o files together. – ProgramMeALife Aug 12 '22 at 17:52
  • make builds the first target in the makefile. The first target in this example is `all`. The prerequisite of `all` is `$(APPNAME)`. That is a program `myapp` and programs require `main`. If you don't want to build the program `myapp` then you should ensure that `all` does not depend in `$(APPNAME)` but instead depends _only_ on what you **do** want to build, which is `$(LIBNAME)`. – MadScientist Aug 12 '22 at 20:04
  • @MadScientist Yeah I figured that out after some trial and error. Now I have another problem and is that I have a file foo.h that includes another bar.h and when the library is compiled the file bar.h seems to be missing. Is anything when foo.o is build causing this? – ProgramMeALife Aug 12 '22 at 20:34
  • Please post a new question for this new issue. StackOverflow is not a forum. – the busybee Aug 12 '22 at 20:38
  • @thebusybee it's the same question I am trying to compile to a static library I am not managing to do so. – ProgramMeALife Aug 12 '22 at 20:42
  • Then please [edit] your question, adding that at first the linker moans about a missing `main`, how you solved it, and at second you got this error with "bar.h". Please make sure to add all commands you launch and the verbatim copies of the error messages. Comments are not for adding new information. – the busybee Aug 12 '22 at 20:49
  • SO is for asking a specific question that has a specific answer, after you've tried to find the answer yourself and not succeeded. It's not really for asking large "how to" questions like "please help me design my build system". Thus, the busybee is correct; this question should rightly have been, "how do I get my makefile to create a static library", and once that was answered you should open a new question "why does the compiler not find my header files", with the proper error messages, command lines, etc. and preferably a minimal test case rather than copying your entire environment. – MadScientist Aug 12 '22 at 20:57
  • @MadScientist Fair enough I will do that next time. Thanks for the help anyways! I'll close the question since I think I found the problem. – ProgramMeALife Aug 12 '22 at 21:03

1 Answers1

1

I ended up adding

...
CXXFLAGS = -std=c99 -Wall -pedantic -lm -I./src
LDFLAGS = -L. -lschemehandler
LIBNAME = libschemehandler
...
$(APPNAME): $(OBJ) $(LIBNAME).a
    $(CC) $(CXXFLAGS) -o $@ $^ $(LDFLAGS)

$(LIBNAME).a: $(OBJ)
    ar rcs $@ $^

lib: $(LIBNAME).a
...

and the library was built correctly. If I call "make lib" I only make the library and if I call "make" I will make the test case for the library as well.