I have tried looking through other answers, unfortunately I have not found anyone with quite the same problem. I am trying to link two C files together, one has a header file, and the other the main file. When trying to make I receive this error:
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
Below is the makefile I use to compile.
INC_DIR = include
SRC_DIR = src
BIN_DIR = bin
UNAME := $(shell uname)
CC = gcc
CFLAGS = -Wall -std=c11 -g -I$(INC_DIR) -I/usr/include/libxml2/
LDFLAGS= -L.
all: XMLParser
XMLParser: $(BIN_DIR)/XMLParser.o $(BIN_DIR)/SVGParser.o
$(CC) $(CFLAGS) -o XMLParser $(BIN_DIR)/XMLParser.o $(BIN_DIR)/SVGParser.o
$(BIN_DIR)/XMLParser.o: $(SRC_DIR)/XMLParser.c $(INC_DIR)/SVGParser.h
$(CC) $(CFLAGS) -o $(BIN_DIR)/XMLParser.o -c $(SRC_DIR)/XMLParser.c
$(BIN_DIR)/SVGParser.o: $(SRC_DIR)/SVGParser.c $(INC_DIR)/SVGParser.h
$(CC) $(CFLAGS) `xml2-config --cflags --libs` -o $(BIN_DIR)/SVGParser.o $(SRC_DIR)/SVGParser.c
clean:
rm *.o XMLParser $(BIN_DIR)/*
XMLParser.c has my main function:
int main(int argc, char **argv) {
...;
return 0;
}
>Solution :
Your recipe for generating the intermediate SVGParser.o is attempting to link and generate a runnable executable. You need to add -c to skip the link step.
You will also need to remove the —libs on that object file and move it to the final link step. In other words add xml2-config —libs for the rule generating the final XMLParser target.
Disclaimer: I’ve never specifically used xml2-config, so I guess there’s some chance I’m wrong here, but it’s a very common paradigm for building and linking against complex libraries, I assume it’s following the same patterns.