I want to have Makefile for my static library where I can conditionally build it as program to perform simple self-test. My current Makefille looks like follows:
OBJECTS = sm3.o ./core/sm3_internal.o
INCLUDES = -I. -I./core
CFLAGS = -g -Wall -O3
CC = c99
OUT = libsm3.a
%.o: %.c
$(CC) -c -o $# $< $(CFLAGS) $(INCLUDES)
$(OUT): $(OBJECTS)
ar rcs $(OUT) $(OBJECTS)
At my main library file I have:
#ifdef TEST
main(int argc, int* argv[])
{
//my self-test logic here
}
#endif
Where should I add -DTEST flag? If I add it to CFLAGS and then add line:
test: $(OBJECTS)
library also would be build with main, which obviously is not something I want.
I would suggest that you compile main.c conditionally instead:
$(TESTOUT): $(OBJECTS) $(TESTOBJ)
$(CC) -o $# $<
Related
I have three files, posl.h, state.c and main.c. What I plan on doing is adding functions that are used throughout the project in posl.h, and then I call it to test the function with main.c, and then make the function in state.c. The problem I am having is that I keep getting an error of undefined reference to init_poslState() even though I have it defined in posl.h
main.c
#include <posl.h>
int main(int argc, char * argv[]) {
pState poslState = init_poslState();
return 0;
}
posl.h
#ifndef POSL_LANGUAGE_H
#define POSL_LANGUAGE_H
#define POSL_MAJOR_VERSION 1
#define POSL_MINOR_VERSION 0
#define POSL_RELEASE_VERSION 0
// State
typedef struct POSL_STATE {
// ...
} pState;
pState init_poslState();
void free_poslState(pState poslState);
#endif
state.c
#include "state.h"
#include <posl.h>
pState init_poslState() {
pState newState;
return newState;
}
Makefile
CFLAGS=-g -Wall -Wextra -I./include
CC=gcc $(CFLAGS)
CORE_O_FILES=./src/Core/lexer.o ./src/Core/parser.o ./src/Core/state.o
CLI_O_FILES=
O_FILES=$(CORE_O_FILES)
# Making CLI Tool
posl: $(CLI_O_FILES) libposl.a ./src/CLI/main.c
$(CC) -o posl -L./ -lposl ./src/CLI/main.c $(CLI_O_FILES)
# Making Library
libposl.a: $(O_FILES) ./include/posl.h
ar rcs libposl.a $^
# Core Files
./src/Core/lexer.o: ./src/Core/lexer.c ./src/Core/lexer.h
$(CC) -o $# -c ./src/Core/lexer.c
./src/Core/parser.o: ./src/Core/parser.c ./src/Core/parser.h
$(CC) -o $# -c ./src/Core/parser.c
./src/Core/state.o: ./src/Core/state.c ./src/Core/state.h
$(CC) -o $# -c ./src/Core/state.c
# PHONY List
.PHONY: all
all:
make update-libs
make libposl.a
make posl
make pcc
# Post-Compile Clean
.PHONY: pcc
pcc:
rm -rf ./src/Core/*.o
rm -rf ./src/CLI/*.o
.PHONY: clean
clean:
make pcc
rm -rf ./libposl.a ./posl*
The order of compiler and (especially) linker options is significant. With this command ...
$(CC) -o posl -L./ -lposl ./src/CLI/main.c $(CLI_O_FILES)
... the linker will not attempt to resolve any function references from main.c against functions in libposl.a. It will look only to objects and libraries appearing after main.c on the command line.
Therefore, rewrite that recipe to
$(CC) -o posl -L. ./src/CLI/main.c $(CLI_O_FILES) -lposl
Welp, #user17732522 answered my question. I had the -l flag messed up, and it wasn't after my source files. ~Thank you guys!~
I am new to makefile and am trying to compile several files.
First is called s-chat and the others are RWT (reader-writer) and DP (dining philosopher). They both use my list library, but s-chat needs it to compile with the -m32 flags.
This is what I tried to do: I called one libMonitor.a using lib-adders.o, lib_movers.o, lib_removers.o. The other one is liblist_32 using lib_adders_32.o, lib_removers_32.o, lib_movers_32.o. For some reason, only the first of these two libraries is made, and the other says it cannot find its first dependency.
PTHREADS = /student/cmpt332/pthreads
RTT = /student/cmpt332/rtt
CC = gcc
CFLAGS = -g
CPPFLAGS = -std=c90 -Wall -pedantic
.PHONEY: all clean
ARCH = $(shell uname -sm | tr -d ' ')
ifeq ($(ARCH),SunOS)
ARCH = $(PLATFORM)
PROCESSOR = "$(shell uname -p)"
ifeq ($(PROCESSOR),i386)
ARCH = i86pc
endif
endif
all: s-chat reader-writer-test dining-philosophers-test
RWT_OBJS = reader-writer_$(ARCH).o reader-writer-monitor_$(ARCH).o Monitor_$(ARCH).o libMonitor_$(ARCH).a
reader-writer-test: CPPFLAGS += -I$(PTHREADS)
reader-writer-test: LDFLAGS += -L$(PTHREADS)/lib/$(ARCH)
reader-writer-test: LDLIBS += -lpthreads
reader-writer-test: $(RWT_OBJS)
$(CC) $(LDFLAGS) $^ $(LDLIBS) -o $#
reader-writer_$(ARCH).o: reader-writer.c reader_writer_monitor.h Monitor.h
reader-writer-monitor_$(ARCH).o: reader-writer-monitor.c reader_writer_monitor.h Monitor.h
DP_OBJS = dining-philosophers_$(ARCH).o dining-philosophers-monitor_$(ARCH).o Monitor_$(ARCH).o libMonitor_$(ARCH).a
dining-philosophers-test: CPPFLAGS += -I$(PTHREADS)
dining-philosophers-test: LDFLAGS += -L$(PTHREADS)/lib/$(ARCH)
dining-philosophers-test: LDLIBS += -lpthreads
dining-philosophers-test: $(DP_OBJS)
$(CC) $(LDFLAGS) $^ $(LDLIBS) -o $#
dining-philosophers_$(ARCH).o: dining-philosophers.c dining_philosophers_monitor.h Monitor.h
dining-philosophers-monitor_$(ARCH).o: dining-philosophers-monitor.c dining_philosophers_monitor.h Monitor.h
Monitor_$(ARCH).o: Monitor.c Monitor.h
list_adders.o: list_adders.c list.h
list_movers.o: list_movers.c list.h
list_removers.o: list_removers.c list.h
libMonitor_$(ARCH).a: list_adders.o list_movers.o list_removers.o
ar rcs $# $^
list_adders_32.o: CFLAGS += -m32
list_adders_32.o: LDFLAGS += -m32
list_adders_32.o: list_adders.c list.h
list_movers_32.o: CFLAGS += -m32
list_movers_32.o: LDFLAGS += -m32
list_movers_32.o: list_movers.c list.h
list_removers_32.o: CFLAGS += -m32
list_removers_32.o: LDFLAGS += -m32
list_removers_32.o: list_removers.c list.h
liblist_32_$(ARCH).a: list_adders_32.o list_movers_32.o list_removers_32.o
ar rcs $# $^
s-chat: CPPFLAGS += -I$(RTT)/include
s-chat: CFLAGS += -m32
s-chat: LDFLAGS += -L$(RTT)/lib/$(ARCH) -m32
s-chat: LDLIBS += -lRtt -lRttUtils
s-chat: s-chat.o liblist_32_$(ARCH).a
s-chat.o: s-chat.c
%_$(ARCH).o %_32_$(ARCH).o %.o %_32.o: %.c
$(CC) $(CPPFLAGS) $(CFLAGS) -c -o $# $<
%: %.o
$(CC) $(LDFLAGS) $(LDLIBS) $^ -o $#
clean:
rm -rf *.o *.a reader-writer-test dining-philosophers-test s-chat
This is the error I get:
ar rcs libMonitor_Linuxx86_64.a list_adders.o list_movers.o list_removers.o
ar: list_adders.o: No such file or directory
Makefile:66: recipe for target 'libMonitor_Linuxx86_64.a' failed
make: *** [libMonitor_Linuxx86_64.a] Error 1
And if I make s-chat last, libMonitor is made, but I get the exact same error with liblist_32 and it says it can't find list_adders_32.o
Anyone know what's going on and how to fix this?
Thanks in advance!!
https://www.gnu.org/software/make/manual/make.html#Pattern-Intro
If a pattern rule has multiple targets, make knows that the rule’s recipe is responsible for making all of the targets. The recipe is executed only once to make all the targets.
%_$(ARCH).o %_32_$(ARCH).o %.o %_32.o: %.c
$(CC) $(CPPFLAGS) $(CFLAGS) -c -o $# $<
Should be something like
%_$(ARCH).o: %.c
$(CC) $(CPPFLAGS) $(CFLAGS) -c -o $# $<
%_32_$(ARCH).o: %.c
$(CC) $(CPPFLAGS) $(CFLAGS) -c -o $# $<
%.o: %.c
$(CC) $(CPPFLAGS) $(CFLAGS) -c -o $# $<
%_32.o: %.c
$(CC) $(CPPFLAGS) $(CFLAGS) -c -o $# $<
But since it seems like all of your sources are in the same directory anyway, you could just delete these rules completely because make already has a built-in rule for %.o: %.c.
i wrote a makefile with some commands.after running command make got a errors undefined reference to sqrtand undefined reference to exp
my make file :
CFILES = smLe.c iniTra.c le.c res.c uti.c smClass.c ini.c clas.c
OBJECTS = smLe.o iniTra.o le.o res.o uti.o smClass.o ini.o clas.o
OBJECTS1 = smLe.o iniTra.o le.o res.o uti.o
OBJECTS2 = smClass.o ini.o clas.o
CFLAGS = -O -lm
CC = gcc
.c .o:
$(CC) $(CFLAGS) $(OBJECTS) -c $<
p1: $(OBJECTS1)
$(CC) $(CFLAGS) $(OBJECTS1) -o $#
p2: $(OBJECTS)
$(CC) $(CFLAGS) $(OBJECTS2) -o $#
smLe.o: iniTra.h le.h res.h
iniTra.o: iniTra.h
le.o: iniTra.h uti.h le.h
res.o: iniTra.h le.h res.h
smClass.o: ini.h clas.h
ini.o: ini.h clas.h
clas.o: ini.h clas.h
Is it any thing missing in my make file.why this error occur in this make file code?please help me ..thanks
-lm is an LDLIB, you pass it to the linker
CFLAGS = -O
LDLIBS = -lm
CC = gcc
.c .o:
$(CC) $(CFLAGS) $(OBJECTS) -c $<
p1: $(OBJECTS1)
$(CC) $(OBJECTS1) -o $# $(LDLIBS)
Libraries have to appear after object files that call what is in them on the command line.
I'm having trouble linking the sha library with my makefile while compiling.
Here is my makefile:
CFLAGS= -g -Wall -Werror -std=c99 -pedantic
LDFLAGS=-lssl -lcrypto
CC = gcc
LD = gcc
OBJS = dhtnode.o
PROG = dhtnode
.c.o:
gcc $< -o $# $(CFLAGS)
all: $(PROG)
$(PROG): $(OBJS)
$(LD) $(LDFLAGS) $(OBJS) -o $(PROG)
dhtnode.o: dhtnode.c dhtpackettypes.h
$(CC) $(CFLAGS) $(LDFLAGS) dhtnode.c
clean:
/bin/rm -f *.o dhtnode
My function using the lcrypto library is here:
#include <openssl/sha.h>
#include <stdlib.h>
#include <stdin.h>
//there are other includes but not concerning this part of the code
char sha() {
char *ibuf = malloc(sizeof(char));
ibuf ="172.0.0.1:11112";
char *obuf = malloc(SHA_DIGEST_LENGTH);
SHA1((unsigned char*)ibuf, strlen(ibuf), (unsigned char*)obuf);
int i;
for (i = 0; i < 20; i++) {
printf("%x" , (unsigned char)obuf[i]);
}
printf("\n");
return *ibuf;
}
Here is the error I get when building with Eclipse:
C/p2p/dhtnode.c:107: undefined reference to `SHA1'
Can anybody tell my what is wrong with my makefile or possible eclipse settings?
Thx in advance!
When compiling the object file, you don't need the LDFLAGS. You'll also need the -c compiler flag to produce an object file instead of linking a binary:
dhtnode.o: dhtnode.c dhtpackettypes.h
$(CC) $(CFLAGS) -c dhtnode.c
After making this change, the program compiles and links successfully for me.
I'm struggling with compiling multiple files into a common program. I'm getting an error:
undefined reference to 'pi'
Here's the skeleton of my code and Makefile. What am I doing wrong? Thanks!
File: calcPi.c
#include <stdlib.h>
#include <stdio.h>
#include <sched.h>
#include <string.h>
#include "pi.h"
int main(int argc, char* argv[]) {
long iterations = 1000000;
int policy = 2;
int numChildren = 3;
pi(iterations, policy, numChildren);
return 0;
}
File: pi.h
void pi(long iterations, int policy, int numChildren);
File: pi.c
#include "pi.h"
void pi(long iterations, int policy, int numChildren) {
//lots of code here
}
I'm compiling this using a Makefile:
CC = gcc
CFLAGS = -c -g -Wall -Wextra
LFLAGS = -g -Wall -Wextra
all: calcPi pi
calcPi: calcPi.o
$(CC) $(LFLAGS) $^ -o $# -lm
pi: pi.o
$(CC) $(LFLAGS) $^ -o $# -lm
calcPi.o: calcPi.c
$(CC) $(CFLAGS) $<
pi.o: pi.c
$(CC) $(CFLAGS) $<
clean:
rm -f pi calcPi
rm -f *.o
rm -f *~
EDIT: In response to the request for the entire error message:
In function 'main'"
calcPi.c:55: undefined reference to 'pi'
collect2: error: ld returned 1 exit status
make: * [calcPi.o] error 1
First of all, is pi really supposed to be a separate application?
You're referring the pi() function from calcPi, but it's only been compiled into pi.o, so you need to add it as a dependency.
What I think you want to do, is to create calcPi using the calcPi.o and pi.o object files.
CC = gcc
CFLAGS = -c -g -Wall -Wextra
LFLAGS = -g -Wall -Wextra
all: calcPi
calcPi: calcPi.o pi.o
$(CC) $(LFLAGS) $^ -o $# -lm
calcPi.o: calcPi.c
$(CC) $(CFLAGS) $<
pi.o: pi.c
$(CC) $(CFLAGS) $<
clean:
rm -f calc
rm -f *.o
rm -f *~