I am trying to make the following makefile on olaris SPARC
CC=/usr/sfw/bin/gcc
INCPATH=/export/home/compkvar/compile/inc
LIBPATH=/export/home/compkvar/compile/lib
SOURCEPATH=/export/home/compkvar/compile/src
.c.o:
$(CC) -c $< $(INCPATH) -I.
PSOURCE = driver.c
driver : driver.o
$(CC) -I$(INCPATH) -L$(LIBPATH) -o driver driver.c -lprivate
all : driver
clean :
rm driver.o driver
but i got error the following ;
/usr/sfw/bin/gcc -I/export/home/compkvar/compile/inc -L/export/home/compkvar/compile/lib -o driver driver.c -lprivate
Undefined first referenced
symbol in file
isinf /export/home/compkvar/compile/lib/libprivate.so
ld: fatal: Symbol referencing errors. No output written to driver
collect2: ld returned 1 exit status
*** Error code 1
make: Fatal error: Command failed for target `driver'
libprivate.so shared library is exist under '/export/home/compkvar/compile/lib' path but it couldn't found:
bash-3.00$ pwd
/export/home/compkvar/compile/lib
bash-3.00$ ls -latr
total 14116
drwxr-xr-x 2 compkvar other 512 Sep 12 15:34 .
drwxr-xr-x 5 compkvar other 512 Sep 12 15:34 ..
-rwxrwxrwx 1 compkvar other 761180 Sep 12 15:43 libprivate.so
-rwxrwxrwx 1 compkvar other 2275492 Sep 12 15:43 libPricingFunctions.so
-rwxrwxrwx 1 compkvar other 1104576 Sep 12 15:43 libprfUtilities.a
-rwxrwxrwx 1 compkvar other 2275492 Sep 12 15:43 libPricingFunctions.so.1
-rwxrwxrwx 1 compkvar other 761180 Sep 12 15:43 libprivate.so.1
bash-3.00$
Thanks in advance.
isinf is part of math library. Try including -lm.
$(CC) -I$(INCPATH) -L$(LIBPATH) -o driver driver.c -lprivate -lm
Related
I'm trying to build a Makefile for my code and for some reason I'm getting
./ex2_q1 11 24 36 7 5
There were 3 prime numbers
make: *** [Makefile:11: test] Error 3
The second line is the output of the program.
Everything seems to work Ok but I don't know why I'm getting the error.
the Makefile is:
PROG = ex2_q1
all: test
test: ex2_q1
./ex2_q1 11 24 36 7 5
factors.o: factors.c
gcc -Wall -c factors.c
ex2_q1.o: ex2_q1.c
gcc -Wall -c ex2_q1.c
clean:
rm -vf *.o $(PROG)
rm -vf *.o factors
rm -vf *.txt
rm -vf *.log
factors: factors.o
gcc -o factors -Wall factors.o
ex2_q1: ex2_q1.o factors
gcc -o ex2_q1 -Wall ex2_q1.o
Your program, ex2_q1 invoked as ./ex2_q1 11 24 36 7 5 returns a non-zero result. This signifies an error and the proper way to address that is to fix your program. If you cannot fix the program you can silence the error by changing the recipe to ./ex2_q1 11 24 36 7 5 || true. If you still want the error message but continue the build you can run make with -k flag, or you can prefix the recipe with a -:
test: ex2_q1
- ./ex2_q1 11 24 36 7 5
Having a library and a main test driver in C, but although the library could be compiled successfully, the main test driver can't.
Here are the directory structures:
qxu#xqiang-mac-1:~/test/test_makefile$ ll
total 0
drwxr-xr-x 3 qxu staff 96 Nov 23 13:19 include
drwxr-xr-x 6 qxu staff 192 Nov 23 13:31 print_ascii_tree
drwxr-xr-x 5 qxu staff 160 Nov 23 13:33 test_main_driver
qxu#xqiang-mac-1:~/test/test_makefile$ ll include/
total 8
-rw-r--r-- 1 qxu staff 706 Nov 23 12:12 print_ascii_tree.h
qxu#xqiang-mac-1:~/test/test_makefile$ ll print_ascii_tree/
total 32
-rw-r--r-- 1 qxu staff 250 Nov 23 13:31 Makefile
-rw-r--r-- 1 qxu staff 9122 Nov 23 12:12 print_ascii_tree.c
qxu#xqiang-mac-1:~/test/test_makefile$ ll test_main_driver/
total 16
-rw-r--r--# 1 qxu staff 616 Nov 23 13:33 Makefile
-rw-r--r--# 1 qxu staff 918 Nov 23 11:51 main.c
Here is the makefile for the library:
qxu#xqiang-mac-1:~/test/test_makefile/print_ascii_tree$ cat Makefile
AR = ar -rcs
RM = rm -rf
CC = gcc
CFLAGS = -I../include
libprint_ascii_tree.a: print_ascii_tree.o
$(AR) libprint_ascii_tree.a print_ascii_tree.o
print_ascii_tree.o: print_ascii_tree.c
$(CC) $(CFLAGS) -c print_ascii_tree.c
clean:
$(RM) *.o *.a
This library could be compiled smoothly:
qxu#xqiang-mac-1:~/test/test_makefile/print_ascii_tree$ make
gcc -I../include -c print_ascii_tree.c
ar -rcs libprint_ascii_tree.a print_ascii_tree.o
qxu#xqiang-mac-1:~/test/test_makefile/print_ascii_tree$ ll
total 64
-rw-r--r-- 1 qxu staff 250 Nov 23 13:31 Makefile
-rw-r--r-- 1 qxu staff 7672 Nov 23 13:38 libprint_ascii_tree.a
-rw-r--r-- 1 qxu staff 9122 Nov 23 12:12 print_ascii_tree.c
-rw-r--r-- 1 qxu staff 7128 Nov 23 13:38 print_ascii_tree.o
Here is the Makefile for the test driver:
qxu#xqiang-mac-1:~/test/test_makefile/test_main_driver$ cat Makefile
program_NAME := main.out
SRCS = main.c
OBJS := ${SRCS:.c=.o}
CC = gcc
CFLAGS = -fsanitize=address -Wall -Wextra -g
program_INCLUDE_DIRS := ../include
program_LIBRARY_DIRS := ../print_ascii_tree
program_LIBRARIES := print_ascii_tree
CFLAGS += $(foreach includedir,$(program_INCLUDE_DIRS),-I$(includedir))
LDFLAGS += $(foreach librarydir,$(program_LIBRARY_DIRS),-L$(librarydir))
LDFLAGS += $(foreach library,$(program_LIBRARIES),-l$(library))
LINK.c := $(CC) $(CFLAGS) $(LDFLAGS)
.PHONY: all
all: $(program_NAME)
$(program_NAME): $(OBJS)
$(LINK.c) $(program_OBJS) -o $(program_NAME)
clean:
rm -rf *.o *.out
This is the error:
qxu#xqiang-mac-1:~/test/test_makefile/test_main_driver$ make
gcc -fsanitize=address -Wall -Wextra -g -I../include -c -o main.o main.c
gcc -fsanitize=address -Wall -Wextra -g -I../include -L../print_ascii_tree -lprint_ascii_tree -o main.out
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [main.out] Error 1
qxu#xqiang-mac-1:~/test/test_makefile/test_main_driver$ ll
total 32
-rw-r--r--# 1 qxu staff 617 Nov 23 13:39 Makefile
-rw-r--r--# 1 qxu staff 908 Nov 23 13:40 main.c
-rw-r--r-- 1 qxu staff 6496 Nov 23 13:40 main.o
Looks like main.o got compiled, but can't be linked with the library into the main executable?
Not sure what's wrong in the Makefile.
P.S. This is what's in main.c:
qxu#xqiang-mac-1:~/test/test_makefile/test_main_driver$ cat main.c
#include "print_ascii_tree.h"
int main()
{
printf("asdf\n");
// A sample use of these functions. Start with the empty tree
// insert some stuff into it, and then delete it
NodePtr root;
root = NULL;
make_empty(root);
printf("\nAfter inserting key 10..\n");
root = insert(10, root);
print_ascii_tree(root);
printf("\nAfter inserting key 5..\n");
root = insert(5, root);
print_ascii_tree(root);
printf("\nAfter inserting key 15..\n");
root = insert(15, root);
print_ascii_tree(root);
printf("\nAfter inserting keys 9, 13..\n");
root = insert(9, root);
root = insert(13, root);
print_ascii_tree(root);
printf("\nAfter inserting keys 2, 6, 12, 14, ..\n");
root = insert(2, root);
root = insert(6, root);
root = insert(12, root);
root = insert(14, root);
print_ascii_tree(root);
make_empty(root);
}
Here:
$(program_NAME): $(OBJS)
$(LINK.c) $(program_OBJS) -o $(program_NAME)
you use program_OBJS variable, which is not defined. If you fix it to $(OBJS) or $^, then there is another problem with LINK.c. It is defined as:
LINK.c := $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS)
which means the resulting command will have library first and main.o second:
-lprint_ascii_tree main.o
and the linker won't find symbols used in main.o, which are defined in the library.
I'm not sure if you can make it work using separate LINK.c variable, but this should work:
$(program_NAME): $(OBJS)
$(CC) $(CFLAGS) $(CPPFLAGS) $^ $(LDFLAGS) -o $#
Hello everyone.
I am trying to compile simple C code.
#include <stdio.h>
void main()
{
printf("Hello world\n");
}
But I get this error.
$ gcc main.c
/data/data/com.termux/files/usr/bin/ld: /data/data/com.termux/files/usr/bin/../lib/gcc/aarch64-unknown-linux-gnu/10.2.0/libgcc.a(lse-init.o): undefined reference to symbol '__getauxval##GLIBC_2.17'
/data/data/com.termux/files/usr/bin/ld: /data/data/com.termux/files/usr/lib/libc.so.6: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
I understand that the error occurs due to the fact that the compidator cannot find the font, but this is not accurate. And the same error occurs in C++.
How do I fix this error?
In short, this error occurs due to the fact that libraries of type libc were not in the common directory.
These libraries:
-rwxr-xr-x 1 10504 10504 1827624 Aug 4 16:27 /lib/conlib/libc-2.32.so
-rw-r--r-- 1 10504 10504 4592750 Aug 4 16:27 /lib/conlib/libc.a
-rw-r--r-- 1 10504 10504 351 Aug 4 16:27 /lib/conlib/libc.so
lrwxrwxrwx 1 10504 10504 18 Aug 4 16:27 /lib/conlib/libc.so.6 -> libc-2.32.so
-rw-r--r-- 1 10504 10504 24964 Aug 4 16:27 /lib/conlib/libc_nonshared.a
I'm having a lot of trouble putting together my c program. This particular issue seems to stem from the linker phase of my compilation. I've spent a lot of time looking at how to do this properly, and I'm out of ideas now. Hopefully, someone here can tell me what I'm missing or what I've put in the wrong place.
This is the particular error, which occurs after giving the make all command:
gcc readers_writers.c bin/readers_writers -o readers_writers
readers_writers.c:13:23: fatal error: rtthreads.h: No such file or directory
#include "rtthreads.h"
^
compilation terminated.
<builtin>: recipe for target 'readers_writers' failed
Here are the files in my working directory:
monitor.h monitor.c readers_writers.c reader_writer_monitor.c reader_writer_monitor.h Makefile
This is the path and file which I need to include. Its a real-time threads package.
/student/cmpt332/rtt/include/rtthreads.h
this is the path to the library:
/student/cmpt332/rtt/lib/Linuxx86_64/libRtt.a
This is the relevant content of my Makefile:
119 RW_C = reader_writer_monitor.c readers_writers.c
120 RW_H = reader_writer_monitor.h
121 RW_OBJ = $(OD)reader_writer_monitor.o $(OD)readers_writers.o
122 $(BIN)readers_writers : $(RW_OBJ) $(LL)libmonitor.a
123 $(CC) $(C_FLAGS) $(RTTIC) -o $# $(RW_OBJ) $(RTTLC) $(MON_LIB)
124 $(OD)reader_writer_monitor.o : reader_writer_monitor.c $(RW_H)
125 $(CC) $(C_FLAGS) -o $# -c $< $(MON_LIB)
126 $(OD)readers_writers.o : readers_writers.c $(RW_H)
137 $(CC) $(C_FLAGS) $(RTTIC) -o $# -c $< $(MON_LIB)
And $(BIN), $(OD) are:
./bin/
./obj/
Here are all the variable declarations:
34 DIR = /student/cmpt332/
35 RTTDIR = $(DIR)rtt/
36 RTTIC = -I$(RTTDIR)include/
37 RTTLC = -L$(RTTDIR)lib/$(CURR_OS)$(ARCH) -m32 -lRtt
From what I've learned ( or what I thought I'd learned, ) the include path $(RTTIC) and the library path $(RTTLC) should be all I need to successfully link the library 'libRtt.a' to my readers-writers program.
I've tried a ton of small variations on the order of where those paths come in my Makefile recipes, but with no success. I know there's lots of threads in different forums and places all over, but I'm at my wits end now, after a few days of trying to figure this out. Any help is appreciated.
Makefile, from popular request:
4 # ...
8 ########################################
9 # Fancy variable debugging tool
10 print-% : ; #echo $* = $($*)
11 ########################################
12 # OS and architecture macros
13 CURR_OS := $(shell uname -s)
14 ARCH := $(shell uname -m)
15 MAC_OS = Darwin
16 LINUX_OS = Linux
17 WIN_OS = MINGW32_NT-6.1
18 ########################################
19 # Select the architecture
20 ifeq ($(ARCH),x86_64)
21 BITS=64
22 else
23 BITS=32
24 endif
25 ########################################
26 # Check if '-static' can be used?
27 ifneq ($(CURR_OS), $(MAC_OS))
28 STATIC = -static
29 else
30 STATIC =
31 endif
32 #######################################
33 # THREADS
34 DIR = /student/cmpt332/
35 UBCDIR = $(DIR)pthreads/
36 RTTDIR = $(DIR)rtt/
37 # RTT THREADS
38 RTTIC = -I$(RTTDIR)include/
39 RTTLC = -L$(RTTDIR)lib/$(CURR_OS)$(ARCH) -m32 -lRtt
40 RTTLCUTILS = -L$(RTTDIR)lib/$(CURR_OS)$(ARCH) -m32 -lRttUtils
41 # UBC PTHREADS
42 UBCC = l$(UBCDIR) -m32
43 UBCL = -L$(UBCDIR)/lib/$(CURR_OS)$(ARCH) -m32 -lpthreads
44 UBCI = -I$(UBCDIR)
45
46 ########################################
47 BIN = ./bin/
48 LL = ./lib/$(CURR_OS)$(ARCH)/
49 OD = ./obj/$(CURR_OS)$(ARCH)/
50 LIST_LIB = -L$(LL) -llist
51 MON_LIB = -L$(LL) -lmonitor
52 ########################################
53 # Compiler and linker options
54 CC = gcc
55 AR_OPTIONS = cr
56 C_FLAGS = -Wall -pedantic -g -m32
57 LD_FLAGS = -m32
58 ########################################
59 # Recipes
60 .PHONY: all clean
61
62 liblist: $(LL)liblist.a
63 test_list: $(BIN)my_test_list
64 libmonitor: $(LL)libmonitor.a
65 readers_writers: $(BIN)readers_writers
66 all: builddirs liblist test_list libmonitor readers_writers
67 ########################################
68 # build the subdirs for libraries and
69 # object files
70 builddirs:
71 mkdir -p $(LL)
72 mkdir -p $(OD)
73 mkdir -p $(BIN)
74
75 #=======================================
76 # LIST LIB
77 #=======================================
78 LIST_C = list_adders.c list_movers.c list_removers.c list.c
79 LIST_H = list.h
80 LIST_OBJ = $(OD)list_adders.o $(OD)list_movers.o $(OD)list_removers.o $(OD)list.o
81 # LIST library
82 $(LL)liblist.a : $(LIST_OBJ)
83 ar $(AR_OPTIONS) $# $^
84 # LIST LIBRARY OBJ FILES
85 $(OD)list.o : list.c $(LIST_H)
86 $(CC) $(C_FLAGS) -c $< -o $#
87 $(OD)list_adders.o : list_adders.c $(LIST_H)
88 $(CC) $(C_FLAGS) -c $< -o $#
89 $(OD)list_movers.o : list_movers.c $(LIST_H)
90 $(CC) $(C_FLAGS) -c $< -o $#
91 $(OD)list_removers.o : list_removers.c $(LIST_H)
92 $(CC) $(C_FLAGS) -c $< -o $#
93 #=======================================
94 # TEST LIST
95 #=======================================
96 LIST_TEST_C = my_test_list.c
97 LIST_TEST_OBJ = $(OD)my_test_list.o
98 $(BIN)my_test_list : $(LIST_TEST_OBJ) $(LL)liblist.a
99 $(CC) $(C_FLAGS) $(LIST_TEST_OBJ) $(LIST_LIB) -o $#
100 $(OD)my_test_list.o : $(LIST_TEST_C) $(LIST_H)
101 $(CC) $(C_FLAGS) -c $< -o $#
102 #=======================================
103 ...
107 #=======================================
108 MON_C = monitor.c
109 MON_H = monitor.h
110 MON_OBJ = $(OD)monitor.o
111 # MONITOR library
112 $(LL)libmonitor.a : $(MON_OBJ)
113 ar $(AR_OPTIONS) $# $^
114 $(OD)monitor.o : $(MON_C) $(MON_H)
115 $(CC) $(C_FLAGS) -o $# -c $<
116 #=======================================
117 # READERS WRITERS
118 #=======================================
119 RW_C = reader_writer_monitor.c readers_writers.c
120 RW_H = reader_writer_monitor.h
121 RW_OBJ = $(OD)reader_writer_monitor.o $(OD)readers_writers.o
122 # readers_writers
123 $(BIN)readers_writers : $(RW_OBJ) $(LL)libmonitor.a
124 $(CC) $(C_FLAGS) $(RTTIC) -o $# $(RW_OBJ) $(RTTLC) $(MON_LIB)
125 # reader_writer_monitor.o
126 $(OD)reader_writer_monitor.o : reader_writer_monitor.c $(RW_H)
127 $(CC) $(C_FLAGS) -o $# -c $< $(MON_LIB)
128 # readers_writers.o
129 $(OD)readers_writers.o : readers_writers.c $(RW_H)
130 $(CC) $(C_FLAGS) $(RTTIC) -o $# -c $< $(MON_LIB)
When I compile without this recipe,
122 # readers_writers
123 $(BIN)readers_writers : $(RW_OBJ) $(LL)libmonitor.a
124 $(CC) $(C_FLAGS) $(RTTIC) -o $# $(RW_OBJ) $(RTTLC) $(MON_LIB)
the .o files readers_writers.o and reader_writer_monitor.o compile just fine. When I run it the readers_writers recipe like so:
22 # readers_writers
123 $(BIN)readers_writers : $(RW_OBJ) $(LL)libmonitor.a
124 $(CC) $(C_FLAGS) -o $# $(RW_OBJ)
I get a whole load of undefined references in both .o files to function in my $(MON_LIB) and the real-time threads library. When I compile with the recipe like THIS:
122 # readers_writers
123 $(BIN)readers_writers : $(RW_OBJ) $(LL)libmonitor.a
124 $(CC) $(C_FLAGS) -o $# $(RW_OBJ) $(MON_LIB) $(RTTIC) $(RTTLC) $(MON_LIB)
I get the single fatal error which tells me there is no such file "rtthreads.h"
I'm seriously at a loss, still.
RESOLVED
Okay, finally figured it out! After switching up my directory variables ( $(OD), $(BIN), etc ) to not include the trailing slash, and making sure my -I and -L directives were in the proper place, I made a couple very small changes which got things compiling properly.
this:
65 readers_writers: $(BIN)readers_writers
66 all: builddirs liblist test_list libmonitor readers_writers
turned into this:
65 ###
66 all: builddirs liblist test_list libmonitor readers_writers
and the directive here:
123 $(BIN)readers_writers : $(RW_OBJ) $(LL)libmonitor.a
124 $(CC) $(C_FLAGS) $(RTTIC) -o $# $(RW_OBJ) $(RTTLC) $(MON_LIB)
turned into this:
123 readers_writers : $(RW_OBJ) $(LL)libmonitor.a
124 $(CC) $(C_FLAGS) $(RTTIC) -o $# $(RW_OBJ) $(RTTLC) $(MON_LIB)
That was all it took! There's a lesson in there somewhere.
gcc is unable to find a dynamic library if I don't specify its path explicitly. First of all I'm using libmemcached which I've got installed with brew.
17:27:14 shell% ls -la /usr/local/lib/libmemcached*
lrwxr-xr-x 1 user wheel 55 Sep 2 12:42 /usr/local/lib/libmemcached.11.dylib -> ../Cellar/libmemcached/1.0.17/lib/libmemcached.11.dylib
lrwxr-xr-x 1 user wheel 48 Sep 2 12:42 /usr/local/lib/libmemcached.a -> ../Cellar/libmemcached/1.0.17/lib/libmemcached.a
lrwxr-xr-x 1 user wheel 52 Sep 2 12:42 /usr/local/lib/libmemcached.dylib -> ../Cellar/libmemcached/1.0.17/lib/libmemcached.dylib
lrwxr-xr-x 1 user wheel 58 Sep 2 12:42 /usr/local/lib/libmemcachedutil.2.dylib -> ../Cellar/libmemcached/1.0.17/lib/libmemcachedutil.2.dylib
lrwxr-xr-x 1 user wheel 52 Sep 2 12:42 /usr/local/lib/libmemcachedutil.a -> ../Cellar/libmemcached/1.0.17/lib/libmemcachedutil.a
lrwxr-xr-x 1 user wheel 56 Sep 2 12:42 /usr/local/lib/libmemcachedutil.dylib -> ../Cellar/libmemcached/1.0.17/lib/libmemcachedutil.dylib
My hellomemcached.c looks like this:
#include <libmemcached/memcached.h>
int main ()
{
memcached_return_t rc;
memcached_server_st* servers = NULL;
memcached_st* memcached;
// initialize the memcached structure
memcached = memcached_create(NULL);
if (!memcached)
return 0;
}
The compilation with the following command ends with success:
gcc -arch x86_64 /usr/local/lib/libmemcached.dylib -I/usr/local/include -o hellomemcached hellomemcached.c
But if I try to compile it with a path to the folder, which contains the library:
gcc -arch x86_64 -L/usr/local/lib -I/usr/local/include -o hellomemcached hellomemcached.c
I get an error:
Undefined symbols for architecture x86_64:
"_memcached_create", referenced from:
_main in ccYCwHa6.o
ld: symbol(s) not found for architecture x86_64
Apparently it can't find the library in that case. What I'm doing wrong?
Add -lmemcached at the very end when compiling.