Apache Avro C Installation - c

I am working on a project and I am using Apache Avro. I have downloaded Apache Avro for C and I followed the instructions provided in order to install it on my system (Ubuntu Linux v14.04). After the installation, I have some header files under the /include directory and some libraries under /lib directory. All of those are the ones that were installed from Apache Avro.
At this point, I have created my C source files which are as follows:
1) socket_client.h :
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include "avro.h"
#include <errno.h>
#include <stdint.h>
#ifndef SOCKET_CLIENT_H_
#define SOCKET_CLIENT_H_
void init_schema(void);
int client_execution_connect(char* ip_addr, int port, char* type);
#endif /* SOCKET_CLIENT_H_ */
2) socket_client.c :
#include <stdio.h>
#include "socket_client.h"
avro_schema_t bigpeer_schema;
void init_schema(void)
{
if( avro_schema_from_json_literal(BIG_PEER_SCHEMA, &bigpeer_schema) )
{
printf("Unable to parse big_peer schema");
exit(EXIT_FAILURE);
}
}
int client_execution_connect(char* ip_addr, int port, char* type)
{
...
}
and a test main file. Also, I have created the following makefile to compile my code:
CC=gcc
CFLAGS=-c -Wall
LDFLAGS=
SOURCES=test_main.c socket_client.c
OBJECTS=$(SOURCES:.c=.o)
EXECUTABLE=avro_test
INC_PATH=/include/avro/
INC=-I/include
LIB=-L/lib
all: $(SOURCES) $(EXECUTABLE)
$(EXECUTABLE): $(OBJECTS)
$(CC) $(LDFLAGS) $(LIB) $(OBJECTS) -o $#
.c.o:
$(CC) $(INC) $(CFLAGS) $< -o $#
clean:
rm -rf *.o avro_test
But, when I try to make my application, I get the following:
nick#rethimno:~/Downloads/AvroClient$ make
gcc -I/include -c -Wall test_main.c -o test_main.o
test_main.c: In function ‘main’:
test_main.c:22:6: warning: unused variable ‘port’ [-Wunused-variable]
int port = atoi(argv[2]);
^
test_main.c:15:8: warning: unused variable ‘type’ [-Wunused-variable]
char* type = "db_node";
^
gcc -I/include -c -Wall socket_client.c -o socket_client.o
gcc -L/lib test_main.o socket_client.o -o avro_test
socket_client.o: In function `init_schema':
socket_client.c:(.text+0x14): undefined reference to `avro_schema_from_json_length'
collect2: error: ld returned 1 exit status
make: *** [avro_test] Error 1
What am I doing wrong? I am not quite certain if the libraries of Apache Avro are loaded properly.
Thank you,
Nick

You are including the Avro headers, but you aren't linking your final executable against the Avro libraries. Assuming you have libavro.so or libavro.a in the lib directory (where *.so is a shared library, and *.a is a static library), you will want to change this line of your Makefile:
LDFLAGS=-lavro
Note that, if the library binary is called something other than libavro.so or libavro.a, you'll need to change the -lavro value to match. Also note that certain packages can contain more than one shared library you will need to link against. I am not familiar enough with Apache Avro to say whether or not this is the case; you will mostly just need to look and see what is inside your lib directory.
You can more information about linking to libraries in the GCC documentation.

Related

Easy gnu make makefile rule to build of GCC executable from multiple source files

I'm trying to make a simple as possible make file that uses the math library (fmax below, the rest is C cruft for this examlpe):
easy.c:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(int argc, const char *argv[]) {
double x=atof(argv[1]);
double y=atof(argv[2]);
double z=fmax(x,y);
printf("max(%f,%f)=%f\n",x,y,z);
return 0;
}
Makefile:
CFLAGS=-g
LDFLAGS=-lm
easy : easy.c
However this creates a link error (missing fmax). This is because make places the LDFLAGS first in the compile line:
> make easy
cc -g -lm easy.c -o easy
/usr/bin/ld: /tmp/ccmN5G9c.o: in function `main':
/home/user/projects/easy/easy.c:8: undefined reference to `fmax'
collect2: error: ld returned 1 exit status
make: *** [<builtin>: easy] Error 1
Of course, if I explicitly put the LDFLAGS at the end using an explicit rule, it works:
CFLAGS=-g
LDFLAGS=-lm
easy : easy.c
$(CC) $(CFLAGS) -o $# $^ $(LDFLAGS)
Of course, understanding the Makefile is not nearly so easy now. Does anyone have a reason why the default rule does not put the link line at the end? Or is there an "easy" make that does allow a newcomer to link a number of source files into one executable (no lingering .o files)?
You should be using LDLIBS not LDFLAGS. LDFLAGS is for linker flags (such as -L). LDLIBS is for linker libraries (such as -lm).
If you investigate the default rules (make -pf/dev/null) you'll find this one:
LINK.o = $(CC) $(LDFLAGS) $(TARGET_ARCH)
%: %.o
# recipe to execute (built-in):
$(LINK.o) $^ $(LOADLIBES) $(LDLIBS) -o $#
(Ignore LOADLIBES as that's a deprecated name). Also see the documentation for LDLIBS and LDFLAGS.

MakeFile: Implicit file declaration warnings in C

i am new to creating Make File and im running into some error caused by dependencies, basically in each "*.c" file i only include it self for example: Main will only include Main.h. As a result of this, i am trying to create the dependencies on MakeFile, the dependencies are:
-Main.c uses functions/structs that are declared in memory.h, synchronization.h, client.h, server.h proxy.h and process.h
-memory.c uses functions/structs that are declared in synchronization.h
-process.c uses functions/structs that are declared in client.h, proxy.h and server.h
Some other dependencies are made by #include in the header files.
Some of the errors i have got so far are:
gcc -c process.c
process.c: In function ‘launch_process’:
process.c:25:46: warning: implicit declaration of function ‘execute_client’ [-Wimplicit-function-declaration]
data->client_stats[process_id] = execute_client(process_id, buffers, data, sems);
process.c:30:45: warning: implicit declaration of function ‘execute_proxy’ [-Wimplicit-function-declaration]
data->proxy_stats[process_id] = execute_proxy(process_id, buffers, data, sems);
process.c:33:46: warning: implicit declaration of function ‘execute_server’ [-Wimplicit-function-declaration]
data->server_stats[process_id] = execute_server(process_id, buffers, data, sems);
gcc -c main.c
main.c: In function ‘launch_processes’:
main.c:159:32: warning: implicit declaration of function ‘launch_process’; did you mean ‘launch_processes’? [-Wimplicit-function-declaration]
data->client_pids[i] = launch_process(i, 0, buffers, data, sems);
main.c: In function ‘wait_processes’:
main.c:243:33: warning: implicit declaration of function ‘wait_process’; did you mean ‘wait_processes’? [-Wimplicit-function-declaration]
data->client_stats[i] = wait_process(data->client_pids[i]); //o retorno dos processos client_pids etc ira returnar o return do execute_client sendo que sera o "exit" do processo
PS:I am not allowed to change header files and sorry for the long question by have spent more then 24 hours straight around this and this is not allowing me to test my finished project, Thank you.
Here is the makefile that i have created.
sovaccines: client.o proxy.o server.o process.o main.o memory.o synchronization.o
gcc client.o proxy.o server.o process.o main.o memory.o synchronization.o -o sovaccines -lrt -pthread -g
main.o: main.c ../include/memory.h ../include/synchronization.h ../include/client.h ../include/proxy.h ../include/server.h ../include/process.h
gcc -c main.c
client.o: client.c
gcc -c client.c
memory.o: memory.c ../include/synchronization.h
gcc -c memory.c
process.o: process.c ../include/client.h ../include/proxy.h ../include/server.h
gcc -c process.c
proxy.o: proxy.c
gcc -c proxy.c
server.o: server.c
gcc -c server.c
synchronization.o: synchronization.c
gcc -c synchronization.c
Your warnings simply tell you that you forgot to include the correct header files in your C source files, or that you did not tell the compiler where to find these header files. Example: if in main.c you have something like:
#include "memory.h"
#include "synchronization.h"
#include "client.h"
#include "server.h"
#include "proxy.h"
#include "process.h"
you should pass the -I../include option to gcc. As you are using GNU make your Makefile could then look like this:
SRC := $(wildcard *.c)
OBJ := $(patsubst %.c,%.o,$(SRC))
INCLUDE := ../include
CFLAGS := -g -I$(INCLUDE)
LDLIBS := -lrt -pthread
VPATH := $(INCLUDE)
sovaccines: $(OBJ)
$(CC) $(CFLAGS) $(LDFLAGS) -o $# $^ $(LDLIBS)
main.o: memory.h synchronization.h client.h proxy.h server.h process.h
memory.o: synchronization.h
process.o: client.h proxy.h server.h
%.o: %.c
$(CC) -c $(CFLAGS) $< -o $#
wildcard and patsubst are make functions
CC, CFLAGS, LDLIBS... are Variables Used by Implicit Rules
VPATH defines a Search Path for All Prerequisites
$#, $^, $<... are Automatic Variables
%.o: %.c is a Pattern Rule
Note that there are ways to automatically generate the dependencies of source files on header files. If you are interested, have a look, for example, at this excellent guide: Auto-Dependency Generation.
Note also that make knows already how to compile and link C projects. It has Implicit Rules. This is another way to further simplify your Makefile. In the proposed Makefile above you could, for instance, omit the pattern rule completely and also the recipe of the sovaccines: $(OBJ) rule.

How to include Apache Avro C header files?

I am trying to use Apache Avro C in a program to better understand how it works. I downloaded Apache Avro for C and installed it on my system (CentOS 7.8.2003). I'm trying to run a copy of the example program provided in the Avro documentation.
// avro_test.c
#include <avro.h>
#include <stdio.h>
#include <stdlib.h>
...
My directory structure looks like this:
avro_test.c
Makefile
avro-c-1.10.0
- build
- src
- libavro.a
- libavro.so
- src
- avro.h
- avro
- // Other header files referenced in avro.h
My Makefile is similar to the one provided here and is listed below:
CC=gcc
CFLAGS=-c -Wall
LDFLAGS=-lavro
SOURCES=avro_test.c
OBJECTS=$(SOURCES:.c=.o)
EXECUTABLE=avro_test
INC_PATH=/avro-c-1.10.0/src/
INC=-I/avro-c-1.10.0/src/ -I/avro-c-1.10.0/src/avro
LIB=-L/avro-c-1.10.0/build/src
all: $(SOURCES) $(EXECUTABLE)
$(EXECUTABLE): $(OBJECTS)
$(CC) $(LDFLAGS) $(LIB) $(OBJECTS) -o $#
.c.o:
$(CC) $(INC) $(CFLAGS) $< -o $#
clean:
rm -rf *.o avro_test
When I run make from the outermost directory, I receive the following error:
gcc -I/avro-c-1.10.0/src/ -I/avro-c-1.10.0/src/avro -c -Wall avro_test.c -o avro_test.o
avro_test.c:18:18: fatal error: avro.h: No such file or directory
#include <avro.h>
^
compilation terminated.
make: *** [avro_test.o] Error 1
I've also tried replacing <avro.h> with "avro.h", but the same error appeared.
Any help is appreciated. Thanks!
I figured it out, it was a silly mistake on my part. In the Makefile, I changed the paths from absolute paths to relative paths (INC_PATH=/avro-c-1.10.0/src/ became INC_PATH=./avro-c-1.10.0/src/). Once I changed all the paths the issue disappeared.

New to c, linking error when using a method declared in a header that's included

I'm new to c and I'm trying to use a method from tester in my main.c class.
So here is main.c:
#include <stdbool.h>
#include <stdio.h>
#include "tester.h"
int main(void) {
bool is_valid = isTrue();
}
Here is tester.h:
#include <stdbool.h>
bool isTrue();
Here is tester.c:
#include "tester.h"
bool isTrue() {
return true;
}
And here is what happens when I try to compile:
$ make main tester
gcc -g -O0 -Wall --std=c99 -pedantic -g -O0 main.c -o main
main.c: In function ‘main’:
main.c:7:10: warning: unused variable ‘is_valid’ [-Wunused-variable]
bool is_valid = isTrue();
^
/tmp/ccwIzgJQ.o: In function `main':
/home/paul/CS261/p1-check/mess/main.c:7: undefined reference to `isTrue'
collect2: error: ld returned 1 exit status
make: *** [main] Error 1
My Makefile was provided by my professor. I can post the contents here, but I'm confident it's correct. I know there's a linking error happening here, but why? I included the tester.h file in my main.c, so shouldn't isTrue be defined? Assistance would be greatly appreciated.
Edit: Here is the Makefile:
# Simple Makefile
#
#
# This makefile builds a simple application that contains a main module
# (specified by the EXE variable) and a predefined list of additional modules
# (specified by the MODS variable). If there are any external library
# dependencies (e.g., the math library, "-lm"), list them in the LIBS variable.
# If there are any precompiled object files, list them in the OBJS variable.
#
# By default, this makefile will build the project with debugging symbols and
# without optimization. To change this, edit or remove the "-g" and "-O0"
# options in CFLAGS and LDFLAGS accordingly.
#
# By default, this makefile build the application using the GNU C compiler,
# adhering to the C99 standard with all warnings enabled.
# application-specific settings and run target
EXE=y86
MODS=p1-check.o
OBJS=
LIBS=
default: $(EXE)
test: $(EXE)
make -C tests test
# compiler/linker settings
CC=gcc
CFLAGS=-g -O0 -Wall --std=c99 -pedantic
LDFLAGS=-g -O0
# build targets
$(EXE): main.o $(MODS) $(OBJS)
$(CC) $(LDFLAGS) -o $(EXE) $^ $(LIBS)
%.o: %.c
$(CC) -c $(CFLAGS) $<
clean:
rm -f $(EXE) main.o $(MODS)
make -C tests clean
.PHONY: default clean
Am I missing a part of the command when I try to use the Makefile for linking? It's probably something on my end, but I'm not sure what.
I figured out what I was doing wrong. I should have been saying make main.o when I wanted to link files with it. I wasn't paying enough attention to the build commands in Makefile, it seems. Thanks to kaylum for the clarification between declarations and definitions.

How do I create Makefile for this

Here is my header file
#include <stdio.h>
#include <stdlib.h>
#include "gd.h"
#include "gdfontmb.h"
#include "gdfontl.h"
#include "gdfontg.h"
When I run this program I usually type 'gcc -o test test.o -lm -lpng -lgd'
It works fine for only one .c file, but this is just for testing. I want to link this with others c file in my project (Actually I'm really new to use gd.h)
Here is my Makefile (but It isn't work!!)
ifeq ($(OSTYPE),WINDOWS)
EXECEXT =.exe
COMP =__MINGCC__
PLATFORM =mingw
else
EXECEXT =
COMP =__GCC__
PLATFORM =linux
endif
EXECUTABLES= test$(EXECEXT)
all : $(EXECUTABLES)
test.o : test.c
gcc -c test.c
test$(EXECEXT) : test.o
gcc -o test$(EXECEXT) test.o -lm -lpng -gd
clean :
-rm *.o
-rm $(EXECUTABLES)
Using this Makefile, I got all error about undefined reference to whatever that are in the gd library.
What did I do wrong and How can I fix this?
Your own cc command already gives the answer. You need -lgd, not -gd.
E.g. set in the start:
LIBS=-lm -lpng -lgd
CC=gcc
(the latter can be the full OS-dependent path as well, and then the CC should be part of the OS specific part, and be specified as a full path).
and change the gcc line later to
$(CC) -o test$(EXECEXT) test.o $(LIBS)
And the rule for test.o is (usually) not really needed, as it is a default way to make a .o file from a .c file.

Resources