I have problems compiling a D program that uses SDL using a makefile called from DOS, where SDL is supposed to be wrapped by Derelict. In addition, if I use a pragma inside the test program for linking, it tells me the lib file has error 43, not a valid library file.
How to make it link, and am I using the wrong stack (Visual C/C++) ? I think not because it is looking for lib files.
test program
//pragma(lib, "D:\\Development\\SDL-1.2.15\\lib\\x86\\SDL.lib");
import derelict.sdl.sdl;
void main()
{
DerelictSDL.load();
// now you can call SDL functions
}
makefile
DMD = dmd
WINDRES = windres.exe
LDFLAGS = -O2 -s `sdl-config --libs`
DFLAGS =
RM = rm -f
#OBJS = main.o graphic.o grid.o node.o appicon.opc
SRCS = toh_fractal.d
# graphic.d grid.d node.d appicon.opc
PROG = toh_fractal
DERELICT = D:\\Development\\Derelict2
INCLUDE_DERELICT = $(DERELICT)\\import
LIB_DERELICT = DerelictSDL
LIB_SDL = SDL
#$(DERELICT)\\lib\\
VERS = 0.1.1
.PHONY: clean distclean
all: $(PROG)
$(PROG): $(SRCS)
$(DMD) $(DFLAGS) $(PROG) -I$(INCLUDE_DERELICT) appicon.res -L$(LIB_DERELICT) -L$(LIB_SDL)
appicon.res: appicon.rc sierpinski.ico
windres -i appicon.rc -o appicon.res
distclean:
$(RM) *~ $(OBJS) appicon.opc stdout.txt stderr.txt
clean:
$(RM) *~ $(OBJS) $(PROG) appicon.opc stdout.txt stderr.txt
makefile output
C:\D\D_fractals_of_hanoi>make all
dmd toh_fractal -ID:\\Development\\Derelict2\\import appicon.res -LDerelictSDL -LSDL
OPTLINK (R) for Win32 Release 8.00.12
Copyright (C) Digital Mars 1989-2010 All rights reserved.
http://www.digitalmars.com/ctg/optlink.html
OPTLINK : Warning 9: Unknown Option : NOIDERELICTSDLSDL
toh_fractal.obj(toh_fractal)
Error 42: Symbol Undefined _D8derelict3sdl3sdl12__ModuleInfoZ
toh_fractal.obj(toh_fractal)
Error 42: Symbol Undefined _D8derelict3sdl3sdl11DerelictSDLC8derelict3sdl3sdl17DerelictSDLLoader
--- errorlevel 2
Derelict is meant ot be used with dynamic linking. You should put the right .so or .dll in your application directory and Derelict will find it.
Derelict uses the C dl lib (dlopen, dlclose, dlsym) to dynamically load the shared library, so you have to use dynamic libraries.
Related
A little background for the problem I am having:
I just migrated from Ubuntu Focal Fossa to Mint Cinnamon keeping my home in a partition
libbsd is installed and can be used to compile some test code (I did a "Hello world" test using strlcpy in the main to verify that libbsd was usable)
Basically in order to use this project I have to use a library provided by my school (which compiled without problems in my last system) which is has a testing script that is going to run a test with its own Makefile:
INC=%%%%
INCLIB=$(INC)/../lib
UNAME := $(shell uname)
CFLAGS= -I$(INC) -O3 -I.. -g
NAME= mlx-test
SRC = main.c
OBJ = $(SRC:%.c=%.o)
LFLAGS = -L.. -lmlx -L$(INCLIB) -lXext -lX11 -lm
ifeq ($(UNAME), Darwin)
# mac
CC = clang
else ifeq ($(UNAME), FreeBSD)
# FreeBSD
CC = clang
else
#Linux and others...
CC = gcc
LFLAGS += -lbsd
endif
all: $(NAME)
$(NAME): $(OBJ)
$(CC) -o $(NAME) $(OBJ) $(LFLAGS)
clean:
rm -f $(NAME) $(OBJ) *~ core *.core
re: clean all
mlx would be the name of the library. For who is not familiar with this syntax (I was not) INC=%%%% is going to expand to /usr/include.
The command to compile the test binary expands to:
$ gcc -o mlx-test main.o -L.. -lmlx -L/usr/include/../lib -lXext -lX11 -lm -lbsd
which generates the error
/usr/lib/gcc/x86_64-unknown-linux-gnu/11.3.0/../../../../x86_64-unknown-linux-gnu/bin/ld: cannot find -lbsd
(that path expands to usr/x86_64-unknown-linux-gnu/bin/ld)
Now, this kinda looks like a gcc configuration, in fact I saw something similar in the output of the command gcc --version -v, but only with x86_64-linux-gnu so without the "unkown" part. Also, one of the weird things is that the path that appears in the error actually does not exist in my system.
I already tried reinstalling gcc but with no different result.
I hope the description was clear enough, please let me know if you may need any other detail that I did not think of.
When trying to compile my c code I keep getting basic.c:5:10: fatal error: 'fftw3.h' file not found. I am compiling my c code using MacOS terminal and have Xcode installed.
I'm trying to write some c code which uses the fftw-3 library. fftw-3 has been installed using sudo port install fftw-3 and I have entered port contents fftw-3 which returned:
/opt/local/include/dfftw.h
/opt/local/include/dfftw_threads.h
/opt/local/include/drfftw.h
/opt/local/include/drfftw_threads.h
/opt/local/include/fftw_f77.i
/opt/local/lib/libdfftw.2.dylib
/opt/local/lib/libdfftw.a
/opt/local/lib/libdfftw.dylib
/opt/local/lib/libdfftw_threads.2.dylib
/opt/local/lib/libdfftw_threads.a
/opt/local/lib/libdfftw_threads.dylib
/opt/local/lib/libdrfftw.2.dylib
/opt/local/lib/libdrfftw.a
/opt/local/lib/libdrfftw.dylib
/opt/local/lib/libdrfftw_threads.2.dylib
/opt/local/lib/libdrfftw_threads.a
/opt/local/lib/libdrfftw_threads.dylib
/opt/local/share/info/fftw.info
/opt/local/share/info/fftw.info-1
/opt/local/share/info/fftw.info-2
/opt/local/share/info/fftw.info-3
/opt/local/share/info/fftw.info-4
/opt/local/share/info/fftw.info-5
I have been using a makefile and am trying to work out what needs including in it. At the moment I have:
# define the name of your source file(s)
SRCS = basic.c
# define the name of the object files(s) - we can do this automatically
OBJS = $(SRCS:.c=.o)
# tell MAKE which compiler to use
CCOMP = gcc
# flags for the compiler
# don't forget the -O3
CFLAGS = -Wall -O3 -fstrict-aliasing -Iinclude
#CFLAGS = -c -Wall -Iinclude
# flags for the linker. note -lm for the math library
LDFLAGS = -O3 -lm -L/opt/lib -I/opt/lib -L/opt/local/include -I/opt/lib
# the name of your executable file (the target) - here we put it in the top directory
TARGET = basic
# actions
all: $(OBJS)
$(CCOMP) -o $(TARGET) $(OBJS) $(LDFLAGS)
%.o: %.c
$(CCOMP) -c -o $# $< $(CFLAGS)
# delete all objects and target
clean:
rm -f $(OBJS) $(TARGET)
I'm not sure if the #CFLAGS and #LDFLAGS sections are correct? I would appreciate troubleshooting and any advice on what I need to do to get this working. Thanks!
I am trying to run the Paho MQTT example in C on my raspberry pi: https://www.eclipse.org/paho/clients/c/
I tried just downloading the pre-built binaries and linking to lpaho-mqtt3c
I put that library here: /home/pi/mqtt_C_testing/Eclipse-Paho-MQTT-C-1.3.1-Linux/lib
And I added: LD_LIBRARY_PATH=/home/pi/mqtt_C_testing/Eclipse-Paho-MQTT-C-1.3.1-Linux/lib/
My makefile looks like this:
IDIR = /home/pi/mqtt_C_testing/Eclipse-Paho-MQTT-C-1.3.1-Linux/include
LDIR = /home/pi/mqtt_C_testing/Eclipse-Paho-MQTT-C-1.3.1-Linux/lib
CC = gcc
LD = gcc
CFLAGS = -Wall
LIBS = -lpaho-mqtt3c
PROG_NAME = main
# directories in project
BIN = bin
SRC = src
OBJ = obj
INCLUDE = include
INCLUDES = -I./$(INCLUDE)
all : $(PROG_NAME)
$(PROG_NAME) : $(BIN)/$$#
$(BIN)/% : $(OBJ)/%.o
$(CC) $(CFLAGS) $^ -o $# $(LIBS)
$(BIN)/main : $(addprefix $(OBJ)/, \
main.o)
$(OBJ)/main.o : $(addprefix $(INCLUDE)/, \
MQTTProperties.h MQTTReasonCodes.h MQTTSubscribeOpts.h MQTTClient.h \
MQTTClientPersistence.h MQTTAsync.h)
$(OBJ)/%.o : $(SRC)/%.c
$(CC) $(INCLUDES) $(CFLAGS) -c $< -o $#
.PHONY: clean
clean:
rm -f $(OBJ)/*.o
When I try to build though, gcc cannot find the library:
gcc -Wall obj/main.o -o bin/main -lpaho-mqtt3c
/usr/bin/ld: cannot find -lpaho-mqtt3c
collect2: error: ld returned 1 exit status
make: *** [makefile:26: bin/main] Error 1
My directory structure is like this:
pi#raspberrypi:~/mqtt_C_testing $ ls
bin Eclipse-Paho-MQTT-C-1.3.1-Linux include makefile obj src
And I have main.c (the mqtt example file) in src and I put all the MQTT header files in include.
The library files for mqtt are here: /home/pi/mqtt_C_testing/Eclipse-Paho-MQTT-C-1.3.1-Linux/lib
I'm really not sure if I'm the right track at all here, so any help is appreciated. Thanks!
To get this to compile I did the following:
1) I built the lib by cloning the repo at github.com/eclipse/paho.mqtt.c and ran make then make install. This puts the .so files in /usr/local/lib
2) I put all the .h files in the main folder of my project directory: /home/pi/mqtt_C_testing/
3) I called the paho mqtt synchronous example main.c and have that also in the main directory.
4) I can then simply use: gcc -Wall -o test main.c -lpaho-mqtt3c to build and create an executable called "test"
5) I had success writing a makefile also that is run just with make. I'm a noob to all this, but I found this website (http://www.cs.colby.edu/maxwell/courses/tutorials/maketutor/) really helped with creating the makefile. I have not really had success putting the .h files in their own include directory yet though.
CC = gcc
CFLAGS=-I.
DEPS = MQTTAsync.h MQTTClientPersistence.h MQTTProperties.h MQTTClient.h MQTTReasonCodes.h MQTTSub$
OBJ = main.o
LIBS= -lpaho-mqtt3c
%.o: %.c $(DEPS)
$(CC) -c -o $# $< $(CFLAGS)
mainmake: main.o
$(CC) -o $# $^ $(CFLAGS) $(LIBS)
i'm writing an emulator program and I need a graphics library. I have 4 files, the graphics library GLWF3 is installed in my includes folder. Im using MacOs Yosemite. I can't figure out how to get the makefile working though to include the glfw3 library. Thanks in advance!
Also note the only file including GLWF3 is graphics.h
Makefile
OBJ = graphics.o chip8.o
emulator: $(OBJ)
gcc -o emulator $(OBJ)
graphics.o: graphics.c graphics.h
gcc -c graphics.c
chip8.o: chip8.c chip8.h
gcc -c chip8.c
clean:
rm -f $(OBJ) emulator
To build with a given library, you have to:
tell the compiler how to find library header file
tell the linker which what library at must link.
Compilation
To tell where are the headers, you must pass a -I/path/to/dir option to gcc. Often, the make CFLAGS variable is used to do so:
CFLAGS= -I/path/to/glfw/include/dir
graphics.o: graphics.c graphics.h
gcc -c graphics.c $(CFLAGS)
chip8.o: chip8.c chip8.h
gcc -c chip8.c
Link
To tell linker what library to use, and where it is located, option -L/path/to/sofile and -lthelib are used. Usually in LDFLAGS variable:
Warning: The -l options must come after the files to link (*.o files)
LDFLAGS = -L/path/to/libglfw/lib/dir
# if the so file name is "libglfw3.so", the "-l" option must be "-lglfw3"
LDFLAGS += -lglfw3
emulator: $(OBJ)
gcc -o emulator $(OBJ) $(LDFLAGS)
pkg-config
To not to have to deal with paths, you can use pkg-config tool: This tool will help you to set CFLAGS and LDFLAGS variables. See here for installation instructions..
Hence, you makefile will looks like:
OBJ = graphics.o chip8.o
# calling program "pkg-config" and store result in CFLAGS variable
CFLAGS = $(shell pkg-config --cflags glfw3)
# calling program "pkg-config" and store result in LDFLAGS variable
LDFLAGS = $(shell pkg-config --ldflags glfw3)
emulator: $(OBJ)
gcc -o emulator $(OBJ) $(LDFLAGS)
graphics.o: graphics.c graphics.h
gcc $(CFLAGS) -c graphics.c
chip8.o: chip8.c chip8.h
gcc $(CFLAGS) -c chip8.c
clean:
rm -f $(OBJ) emulator
To make a custom version of the function igraph_get_shortest_paths_dijkstra I made a copy of it from the file:structural_properties.c. I've yanked it out and put it in my local .c file and one of the modifications to my Makefile is the following, but I get an error:
gcc -I/usr/local/include/igraph -I/Users/saguinag/ToolSet/igraph-0.7.1/src -I/Users/saguinag/ToolSet/igraph-0.7.1 -o mycc comp_catpath.o -L/usr/local/lib -ligraph
Undefined symbols for architecture x86_64:
"_saguinag_get_shortest_path", referenced from:
_main in comp_catpath.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [mycc] Error 1
Where my Makefile starts out as:
## Author: Sal Aguinaga (c) 2015
CC=gcc
INCLUDES=-I/usr/local/include/igraph \
-I/Users/saguinag/ToolSet/igraph-0.7.1/src \
-I/Users/saguinag/ToolSet/igraph-0.7.1
LFLAGS=-L/usr/local/lib
LIBS=-ligraph
OUT=comp_catpath
OBJS=comp_catpath.o
# define the C source files
SRCS = comp_catpath.c
OBJS = $(SRCS:.c=.o)
# define the executable file
MAIN = mycc
.PHONY: depend clean
all: $(MAIN)
#echo Simple compiler named mycc has been compiled
$(MAIN): $(OBJS)
$(CC) $(CFLAGS) $(INCLUDES) -o $(MAIN) $(OBJS) $(LFLAGS) $(LIBS)
# this is a suffix replacement rule for building .o's from .c's
# # it uses automatic variables $<: the name of the prerequisite of
# # the rule(a .c file) and $#: the name of the target of the rule (a .o file)
# # (see the gnu make manual section about automatic variables)
.c.o:
$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $#
#
clean:
$(RM) *.o *~ $(MAIN)
#
depend: $(SRCS)
makedepend $(INCLUDES) $^
#
# DO NOT DELETE THIS LINE -- make depend needs it
do I need to specify the architecture? My machine is a MacBook Pro. When I type make -v I get that it's GNU Make 3.81 with the last line saying "This program built for i386-apple-darwin11.3.0" Is there a better way to do this?
You are compiling and linking a single source file, comp_catpath.c. In this file, you have defined the function main, and in main you try to call the function saguinag_get_shortest_path. However, you haven't defined the function saguinag_get_shortest_path in comp_catpath.c and it's also not defined in the igraph library or in the C standard library.
If you already put the definition of saguinag_get_shortest_path in another .c file, you need to include that file's name in the definition of SRCS in your Makefile. If you didn't define saguinag_get_shortest_path anywhere, you need to write a definition for it, either in comp_catpath.c or in a new file (that you then add to SRCS).