GCC: Linking a dylib on Mac Os - c

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.

Related

How to deal with Makefile error: make: *** [Makefile:11: test] Error 3

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

Incorrect C Makefiles to generate library and main program?

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 $#

Error gcc - undefined reference to symbol '__getauxval##GLIBC_2.17'

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

rpcgen only one argument is allowed error

I'm writing a simple client-server calculator with rpcgen and am having a compilation error. Here's the .x file (calculadora.x):
union resultado_calculo switch (int err)
{
case 0:
int resultado;
default:
void;
};
program CALCULADORA_PROG
{
version CALCULADORA_VER
{
resultado_calculo Suma (int, int) = 1;
resultado_calculo Resta (int, int) = 2;
resultado_calculo Producto (int, int) = 3;
resultado_calculo Cociente (int, int) = 4;
} = 1;
} = 20000001;
Running rpcgen -NCa calculadora.x creates the following files:
total 40K
drwxr-xr-x 2 groctel groctel 4.0K Mar 23 16:40 .
drwxr-xr-x 3 groctel groctel 4.0K Mar 23 16:10 ..
-rw-r--r-- 1 groctel groctel 1.3K Mar 23 16:27 calculadora_client.c
-rw-r--r-- 1 groctel groctel 1.9K Mar 23 16:22 calculadora_clnt.c
-rw-r--r-- 1 groctel groctel 1.1K Mar 23 15:56 calculadora_server.c
-rw-r--r-- 1 groctel groctel 3.5K Mar 23 16:22 calculadora_svc.c
-rw-r--r-- 1 groctel groctel 1.2K Mar 23 16:23 Makefile.calculadora
-rw-r--r-- 1 groctel groctel 2.7K Mar 23 16:22 calculadora.h
-rw-r--r-- 1 groctel groctel 348 Mar 23 16:22 calculadora.x
Now, I've filled the client and server's templates and run the program several times. I have to compile with make CFLAGS+="-I/usr/include/tirpc -ltirpc" -f Makefile.calculadora to include my system's rpc library but no real problems there. I've also run the program a few times and all the operations run well (./calculadora 3 + 2 returns 5 for example).
However, I went to make the program again today and I'm getting the following output:
➜ make CFLAGS+="-I/usr/include/tirpc -ltirpc" -f Makefile.calculadora
rpcgen calculadora.x
resultado_calculo Suma (int, int) = 1;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
calculadora.x, line 13: only one argument is allowed
make: *** [Makefile.calculadora:32: calculadora_xdr.c] Error 1
I don't understand why the error is appearing now and not before, since the program was running perfectly just yesterday. Here's the Suma function that I'm compiling:
resultado_calculo *
suma_1_svc(int arg1, int arg2, struct svc_req *rqstp)
{
static resultado_calculo result;
xdr_free(xdr_resultado_calculo, &result);
result.resultado_calculo_u.resultado = arg1 + arg2;
return &result;
}
The other functions are the exact same with different operators (-, * and /). What am I doing wrong?
You need to use the -N option to allow multiple arguments. From the documentation:
-N
Use the newstyle of rpcgen. This allows procedures to have multiple arguments. It also uses the style of parameter passing that closely resembles C. So, when passing an argument to a remote procedure you do not have to pass a pointer to the argument but the argument itself. This behaviour is different from the oldstyle of rpcgen generated code. The newstyle is not the default case because of backward compatibility.
Prior to this feature, the way you passed multiple arguments was by packing them into a structure and passing a pointer to the structure.

How to link shared library in Makefile

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

Resources