Compiling C program that uses a library - c

I've made a C program that uses libsndfile to extract some data from audio files.
What possibilities are there to make the program as portable as possible, preferably without requiring root access when installing?
Libsndfile is not available at the target machines, so i need to somehow package it with my program. Is there a way to statically link the library? I've also looked at some Autotools tutorials, but I'm not sure how to proceed.
I can compile without a hitch on my dev machine, where I installed the libraries using the package manager: apt-get install libsnfile1-dev
The makefile is very simple:
CFLAGS=-std=c99 -Wall -pedantic -g
CLIBS= -lsndfile -lm
BIN=audiodecode
CC=gcc
MAIN=main.o
FILES=
OBJS=$(FILES) $(MAIN)
.PHONY: all
all: clean $(OBJS)
$(CC) $(CFLAGS) -o $(BIN) $(OBJS) $(CLIBS)
.PHONY: clean
clean:
rm -f *.o $(BIN)

A lot of packages bundle their dependencies. Examples: rsync (contains a bundled libpopt), gnupg (contains a bundled libz). Other dependencies commonly bundled are gettext or glib.
For inspiration look at how these popular open source projects do it.
Put the content of http://www.mega-nerd.com/libsndfile/files/libsndfile-1.0.25.tar.gz into a subdir and add apropriate rules to build the the subdir first.
Untested sample code:
OBJS += libsndfile/libsndfile.a
libsndfile/libsndfile.a:
cd libsdndfile && ./configure --enable-static && $(MAKE)
For Bonus points add a configure script, that check if the system has already installed libsndfile and link to it dynamically.

Related

Trouble creating a transferable executable with SDL 1.2 and C

I've been having trouble compiling an executable with SDL 1.2 and C that will work on another machine. I'vee been compiling it from Ubuntu using a makefile. I'd like to be able to send a "bin.zip" to a classmate, with "SDL.dll" and "SDL_ttf.dll" inside as well as "prog" the executable that I compiled for them.
I would expect them to be able to run that "prog" from their own Ubuntu desktop (Virtual Machine, if that's relevant) and not need to install libsdl1.2debian and libsdl-ttf2.0-0 themselves first, since I included the DLLs.
Note that they can run it fine once they've installed those libraries.
My project structure, at compilation, is such:
2048-c
/bin (dlls and executable)
/include (".h" header files)
/lib ("SDL.lib", "SDL_ttf.lib", "SDLmain.lib")
/src (".c" source files)
"makefile"
My makefile looks like this:
CC=gcc
CFLAGS=-c -Wall `sdl-config --cflags`
LDFLAGS=`sdl-config --libs` -lSDL_ttf
SOURCES=src/main.c src/toolbox.c src/game.c
OBJECTS=$(SOURCES:.c=.o)
EXECUTABLE=bin/prog
.PHONY: clean
all: $(SOURCES) $(EXECUTABLE)
$(EXECUTABLE): $(OBJECTS)
$(CC) $(OBJECTS) $(LDFLAGS) -o $#
.c.o:
$(CC) $(CFLAGS) $< -o $#
clean:
rm -f $(OBJECTS) $(EXECUTABLE)
Am I missing something obvious? Edit: (Yes, I was)
Yes, I was missing something obvious : DLLs are specifically for Windows
Now, as for getting an executable to work without installing the required libraries, I still haven't managed that (linking statically didn't pan out, in my case), but at least I learnt something.
If I give cross-compilation to Windows a try, I'll try to update this.

Running library makefile from application makefile

I have an application that uses my library. For both projects I have separate makefiles
My application (source code + Makefile) is in 'client' folder. At the same level I have the library folder 'mylib'.
The make file of the client application looks like this:
LIBS = -L../mylib -lmy
CFLAGS = -Wall -W -I../mylib
TARGET = clientapp
SOURCEDIR = .
SOURCES = $(wildcard $(SOURCEDIR)/*.c)
OBJECTS = $(SOURCES:.c=.o)
all: $(TARGET)
$(TARGET): $(OBJECTS)
$(CXX) $(CFLAGS) -o $(TARGET) $(OBJECTS) $(LIBS) $(LDFLAGS) -Wl,-rpath=../mylib
clean:
rm -f $(OBJECTS) $(TARGET)
Currently to make everything work I need first go to the library folder, call make there, then go to the client application folder and call make here.
What I want to do is to run everything in the client application's makefile.
I found that I need to use 'recursive make'.
But I can't make it build everything. The only library is built.
What is the right syntax for that?
Although recursive make is not the best solution in the long run, it is a viable option if you are just starting out.
At the top level you just have to create a new Makefile that only executes your two existing makefiles
.PHONY: all
all:
$(MAKE) -C mylib
$(MAKE) -C source
(the indents should be tabs, obviously)
This will execute both makefiles in their respective directories.
It is better to include the makefiles and be non-recursive, but that requires more knowledge of make. Take one step at a time.

Get compilation info of an installed program

I need to obtain the information on the C-compiler used to build an installed program. I am guessing a rt or a lib can report that, but nothing concrete. Not that the program would be installed in /usr/... or a similar place, and hence would not have access to the build directory to get the relevant info.
Well behaved programs should understand the --version argument.
Packaged programs (i.e. those installed with dpkg -i or apt-get install of a .deb package on Debian, etc...) also know their package version and source.
You might try to use strings on the binary executable. However, such meta-data (about the version of the C compiler used to build the program) might have been stripped (e.g. by the strip command).
If you are developing the program (i.e. its C source code) and can change it, you might consider adding something like
timestamp.c: Makefile
echo 'const char timestamp[]=' > $#
date +'"built with $(shell $(CC) --version) on %c";' >> $#
yourprogram: $(OBJECTS) timestamp.o
$(LINK.c) $(LDFLAGS) $< -o $# $(LDLIBES)
$(RM) timestamp.c
in your Makefile (details could be wrong, but you get the idea)

how to link dependency libraries with makefile

Dependency Libraries: libxml >= 2.7.6 openssl >= 0.9.8 Digital Signature Generation requires an additional libraries: libXslt >= 1.1.24 libxmlsec1 >= 1.2.9
These are Dependency library. i have project code but for running the makefile required dependency libraries.
I have downloaded these libraries and copy into usr/include folder then installed.
but i dont know the exact way how to add in makefile these dependency library...
i m using ubuntu 11.04.
#GCC compiler
CC=gcc
CFLAGS= -D__XMLSEC_FUNCTION__=__FUNCTION__ -DXMLSEC_NO_XSLT=1 -DXMLSEC_NO_XKMS=1 -I/usr/include/libxml2 -DXMLSEC_CRYPTO_DYNAMIC_LOADING=1 -DXMLSEC_CRYPTO=\"openssl\" -DUNIX_SOCKETS -DXML_SECURITY -DDEBUG -I/usr/include/protobuf-c-0.15 -L/usr/include/libxslt-1.1.24
LDFLAGS= -lcrypto -I/usr/include/libxml2 -lxml2 -I/usr/include/xmlsec1 -lxmlsec1 -lprotobuf-c
LIBS=-lm
fileClient:
$(CC) $(CFLAGS) $(LDFLAGS) $(LIBS) src/main.c src/file2.c src/file3.pb-c.c -o fileClient
clean:
$(RM) fileClient *.o *~
#
Don't do that in the Makefile but in some of your header with code like
#if LIXML_VERSION < 20800
#error too old version of libxml need 2.8.0 or newer
#endif
and you may be interested in autotools (autoconf, automake and friends) and in pkg-config
Also, dependency management is best handled by making a .deb package. (so you want an Ubuntu package like libxml2-dev)

Cross compiling source from windows to linux

This is my make file
EXE = signsrch
CFLAGS += -s -O2
PREFIX = /usr/local
BINDIR = $(PREFIX)/bin
SRC = $(EXE).c
all:
$(CC) $(CFLAGS) -c disasm.c
$(CC) $(CFLAGS) -c asmserv.c
$(CC) $(SRC) $(CFLAGS) -o $(EXE) *.o
install:
install -m 755 -d $(BINDIR)
install -m 755 $(EXE) $(BINDIR)/$(EXE)
.PHONY:
install
I want to cross compile it for my ubuntu and I tried:
make ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi-
but I get unknown type name errors with a bunch of âDWORDâ
Help?
We also need to see some code, but....
Windows has its own types for basic things above the vanilla C ones, as does Linux. It sounds like DWORD as a type is not known in Linux (likely). You'll probably have to create a mytypes.h file that redefines Windows standards like DWORD into Linux speak when building for a Linux platform. Linux has types.h that defines things like int32_t which is the equivalent. See this thread for more about this.
I've assumed you have a working cross compiler set up and you're fighting just with the port. If you haven't, that's your first job. You could have a windows based compiler that targets Linux (the cygwin option, mentioned in another post) or go for a Linux based compiler and targetting windows (crosstool will help here). Though since you seem to be targetting arm, I'm expecting that that Ubuntu install isn't the place you wish to build! :-)
to cross compile you need a toolchain for the target platform, not just a Makefile. Check this tutorial and also Cygwin

Resources