How to include SDL in your program (Linux). undefined reference to SDL_Init() - linux

Im trying everything to run this test Program and keep getting this error messages:
g++ objects/src_files/display.o objects/src_files/main.o -o program -L/usr/local/lib
objects/src_files/main.o: In function `main':
main.cpp:(.text+0x16): undefined reference to `SDL_Init'
collect2: error: ld returned 1 exit status
make: *** [program] Error 1
this is how my test program looks like:
#include <iostream>
#include <string>
#include <GL/glew.h>
#include <SDL2/SDL.h>
#include <glm/glm.hpp>
#include <display.hpp>
using namespace std;
int main(int argc, char **argv)
{
SDL_Init(SDL_INIT_EVERYTHING);
glm::vec3 my_v(0, 0, 0);
string s = "This is a Test";
cout << s << endl;
return 0;
}
and this is my MAKEFILE:
APP = program
CC = g++
SRCDIR = src_files
OBJDIR = objects
H_DIR = header_files
INC_DIR = include
LIB_DIR = /usr/local/lib
SRCS := $(shell find $(SRCDIR) -name '*.cpp')
SRCDIRS := $(shell find . -name '*.cpp' -exec dirname {} \; | uniq)
OBJS := $(patsubst %.cpp,$(OBJDIR)/%.o,$(SRCS))
CFLAGS = -Wall -pedantic -ansi -lSDL
LDFLAGS =
#update here
_H = $(HD_DIR)/display.hpp
SDL_H = $(INC_DIR)/SDL.h
MAIN_H = $(_H) $(SDL_H)
all: $(APP)
$(APP) : buildrepo $(OBJS)
$(CC) $(OBJS) $(LDFLAGS) -o $#
$(OBJDIR)/%.o: %.cpp
$(CC) $(CFLAGS) -I$(INC_DIR) -I$(H_DIR) -c $< -o $#
#update here
$(OBJ_DIR)/main.o: $(MAIN_H)
$(OBJ_DIR)/display.o: $(_H) $(SDL_H)
clean:
$(RM) $(OBJS)
distclean: clean
$(RM) $(APP)
buildrepo:
#$(call make-repo)
define make-repo
for dir in $(SRCDIRS); \
do \
mkdir -p $(OBJDIR)/$$dir; \
done
endef
I run the MAKEFILE with: make -f MAKEFILE
What else can I try?
Thanks

In your Makefile change
CFLAGS = -Wall -pedantic -ansi -lSDL
LDFLAGS =
To
CFLAGS = -Wall -pedantic -ansi
LDFLAGS = -lSDL
-lSDL is actually a linker flag, not compiler.

Related

Can anyone explain me this makefile example?

# Makefile to compare sorting routines
BASE = /home/blufox/base
CC = gcc
CFLAGS = -O –Wall
EFILE = $(BASE)/bin/compare_sorts
INCLS = -I$(LOC)/include
LIBS = $(LOC)/lib/g_lib.a \
$(LOC)/lib/h_lib.a
LOC = /usr/local
OBJS = main.o another_qsort.o chk_order.o \
compare.o quicksort.o
$(EFILE): $(OBJS)
#echo “linking …”
#$(CC) $(CFLAGS) –o $# $(OBJS) $(LIBS)
$(OBJS): compare_sorts.h
$(CC) $(CFLAGS) $(INCLS) –c $*.c
# Clean intermediate files
clean:
rm *~ $(OBJS)
variables substitute to form,
gcc -O –Wall -o /home/blufox/base/bin/compare_sorts main.o another_qsort.o chk_order.o compare.o quicksort.o main.c another_qsort.c chk_order.c compare.c
for eg:
$(OBJS) = main.o another_qsort.o chk_order.o compare.o quicksort.o

dylib dynamic library calling a dylib : Undefined symbols for architecture i386

Under mac os x with g++ from gcc-5.2 I am trying to do the following : create a dylib exporting a class defined by header tmp8bis_dylib.h and source tmp8bis_dylib.cpp, and then create another dylib out of a source file tmp8bis.cpp using and linking to the previous dylib. Header and sources are in the same directory. I compile as follows :
g++-5.2.0 -m32 -Wall -g -c ./tmp8bis_dylib.cpp
g++-5.2.0 -m32 -dynamiclib ./tmp8bis_dylib.o -o ./tmp8bis_dylib.dylib
g++-5.2.0 -m32 -Wall -g -c ./tmp8bis.cpp
g++-5.2.0 -m32 -dynamiclib ./tmp8bis.o -o ./tmp8bis.dylib
and get this :
Undefined symbols for architecture i386:
"complex::cmodule(double, double)", referenced from:
_mymodule in tmp8bis.o
"complex::complex(double, double)", referenced from:
_mymodule in tmp8bis.o
"complex::~complex()", referenced from:
_mymodule in tmp8bis.o
ld: symbol(s) not found for architecture i386
collect2: error: ld returned 1 exit status
make: *** [all] Error 1
Obviously, I tried to pass various include and library paths with -I and -L flags respectively, with the very same result... Any idea ?
Files are below :
For tmp8bis_dylib.h :
#ifndef TMP_8_BIS_DYLIB_H
#define TMP_8_BIS_DYLIB_H
class complex
{
public:
double real;
double imag;
public:
complex();
complex(double x);
complex(double x,double y);
double cmodule(double x, double y);
~complex();
};
#endif
For tmp8bis_dylib.cpp :
#include "./tmp8bis_dylib.h"
#include <math.h>
extern "C"
{
complex::complex()
{
real = 0.0 ;
imag = 0.0 ;
}
complex::complex(double x)
{
real = x ;
imag = 0.0 ;
}
complex::complex(double x,double y)
{
real = x ;
imag = y ;
}
double complex::cmodule(double x, double y)
{
double res = sqrt(x*x+y*y);
return res ;
}
complex::~complex()
{
}
}
For tmp8bis.cpp :
#include <math.h>
#include "./tmp8bis_dylib.h"
extern "C"
{
double mymodule(double x, double y)
{
complex z(x,y);
double ret = z.cmodule(x,y);
return ret;
}
}
Precision. -m32 is because I need 32 bits dylib because the final dylib will be plugged into excel 2011's (for mac) VBA, which is 32 bits.
EDIT. Following Brett Hale's comment about Apple's advises about dylibs, I added
#define EXPORT __attribute__((visibility("default")))
after the #include's from tmp8bis.cpp, and EXPORT's for all its member functions, and compiled as follows :
g++-5.2.0 -m32 -Wall -g -c ./tmp8bis_dylib.cpp
g++-5.2.0 -m32 -dynamiclib ./tmp8bis_dylib.o -fvisibility=hidden -o ./tmp8bis_dylib.dylib
did a sudo cp ./tmp8bis_dylib.dylib /opt/lib/libtmp8bis_dylib.dylib and then compiled :
g++-5.2.0 -m32 -Wall -g -c ./tmp8bis.cpp
g++-5.2.0 -m32 -dynamiclib ./tmp8bis.o -o ./tmp8bis.dylib -L/opt/lib
and got the same result as before... Nor did
g++-5.2.0 -m32 -dynamiclib ./tmp8bis.o -o ./tmp8bis.dylib -ltmp8bis_dylib.dylib
make my day.
Without resorting to #define EXPORT __attribute__((visibility("default"))) or any -fvisibility=hidden
g++-5.2.0 -m32 -Wall -fpic -g -c ./tmp8bis_dylib.cpp
g++-5.2.0 -m32 -shared ./tmp8bis_dylib.o -o ./libtmp8bis_dylib.dylib
g++-5.2.0 -m32 -Wall -g -c ./tmp8bis.cpp
g++-5.2.0 -m32 -shared ./tmp8bis.o -o ./tmp8bis.dylib -L. -ltmp8bis_dylib
finally worked. I did not managed to succeed without -fpic, naming libtmp8bis_dylib.dylib and using -ltmp8bis_dylib.

Module verification failed when insmod a module

On Ubuntu 14.04, Kernel 3.13.0, When I insert below simple module, I got error message from kernel log:
"module verification failed: signature and/or required key missing - tainting kernel"
Did I made any mistake or missed anything?
Here is module source code in a file named ts2.c.
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
#include <linux/init.h>
MODULE_LICENSE("GPL");
MODULE_ALIAS("hello2");
static int __init hello1_init(void)
{
printk(KERN_INFO "Hello world 2.\n");
return 0;
}
static void __exit hello1_exit(void)
{
printk(KERN_INFO "Goodbye world 2.\n");
}
module_init(hello1_init);
module_exit(hello1_exit);
Here is the Makefile:
ifeq ($(DEBUG),y)
DEBFLAGS = -O -g -DPCI_INFO_DEBUG # "-O" is needed to expand inlines
else
DEBFLAGS = -O2
endif
EXTRA_CFLAGS += $(DEBFLAGS) -I$(LDDINC)
ifneq ($(KERNELRELEASE),)
obj-m := ts2.o
else
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) LDDINC=$(PWD) modules
clean:
$(MAKE) -C $(KERNELDIR) M=$(PWD) LDDINC=$(PWD) clean
endif
depend .depend dep:
$(CC) $(EXTRA_CFLAGS) -M *.c > .depend
ifeq (.depend,$(wildcard .depend))
include .depend
endif
You have a problem in your make file...
ifeq ($(DEBUG),y)
DEBFLAGS = -O -g -DPCI_INFO_DEBUG # "-O" is needed to expand inlines
else
DEBFLAGS = -O2
endif
EXTRA_CFLAGS += $(DEBFLAGS) -I$(LDDINC)
ifneq ($(KERNELRELEASE),)
obj-m := ts2.o
Rather it should be as below:
ifeq ($(DEBUG),y)
DEBFLAGS = -O -g -DPCI_INFO_DEBUG # "-O" is needed to expand inlines
else
DEBFLAGS = -O2
endif
EXTRA_CFLAGS += $(DEBFLAGS) -I$(LDDINC)
ifneq ($(KERNELRELEASE),)
obj-m := hello1.o
Now your issues should be solved

OS detection Makefile

Here is my makefile, until today I never try to use OS detection in it. Mine looks like this :
CC = gcc
CFLAGS = -Wall -Wextra -O1 -Wuninitialized
OUT = project.exe
ifeq ($(UNAME),Darwin) #Mac OS
echo "Darwin"
SRC = sdl_gui.c libSDLextra.c libImageProcessing.c SDLmain.m
OBJ = sdl_gui.o libSDLextra.o libImageProcessing.o SDLmain.o
LIBS = -I /Library/Frameworks/SDL.framework/Headers -framework SDL -I /Library/Frameworks/SDL_ttf.framework/Versions/A/Headers -framework SDL_ttf -framework Cocoa
endif
ifeq ($(UNAME),Linux) #Linux based systems
SRC = sdl_gui.c libSDLextra.c libImageProcessing.c
OBJ = sdl_gui.o libSDLextra.o libImageProcessing.o
LIBS = -lSDL -lSDL_ttf
endif
all : $(OUT)
$(OUT) : $(OBJ)
$(CC) $(CFLAGS) $(OBJ) -o $(OUT)
$(OBJ) : $(SRC)
$(CC) $(CFLAGS) -c $(SRC)
clean :
rm -f $(OBJ) $(OUT)
When I do make I've got this error :
gcc -Wall -Wextra -O1 -Wuninitialized -o projet.exe
clang: error: no input files
make: *** [projet.exe] Error 1
I understand the error but I don't know how to fix it.
It looks like you are missing this from (near) the top of the file:
UNAME := $(shell uname)

make issue on Linux

I'm trying to debug an issue with a makefile I am working on.. What is confusing is that the target works when I run it from the command line, but does not work in my makefile..
Here is the makefile:
DDS_OUT_DIR = $(PWD)
IDL_DIR=/opt/idl/dds
IDL_TYPES=common.idl
GENERATED_SOURCES = $(IDL_TYPES:%.idl=%Support.cxx) $(IDL_TYPES:%.idl=%Plugin.cxx) $(IDL_TYPES:%.idl=%.cxx)
GENERATED_HEADERS = $(IDL_TYPES:%.idl=%Support.h) $(IDL_TYPES:%.idl=%Plugin.h) $(IDL_TYPES:%.idl=%.h)
OBJS_DIR = obj.$(CPUTYPE)
GENERATED_OBJS = $(GENERATED_SOURCES:%.cxx=$(OBJS_DIR)/%.o)
LIBDIR = ../../lib.$(CPUTYPE)
BINDIR = ../../../../bin.$(CPUTYPE)
CC = $(C_COMPILER)
CXX = $(CPP_COMPILER)
OS = $(shell uname)
DDSCOMMON = ../../Common/src
CFLAGS = -m32 -g
CXXFLAGS = -m32 -g
LDFLAGS = -m32 -static-libgcc
SYSLIBS = -ldl -lnsl -lpthread -lm -lc
DEFINES_ARCH_SPECIFIC = -DRTI_UNIX
DEFINES = $(DEFINES_ARCH_SPECIFIC) $(cxx_DEFINES_ARCH_SPECIFIC)
INCLUDES = -I. -I$(NDDSHOME)/include -I$(NDDSHOME)/include/ndds
INCLUDES += -I$(DDSCOMMON)
LIBS = -L$(NDDSLIBDIR) -L$(LIBDIR) -lrt \
-lnddscppz -lnddscz -lnddscorez $(SYSLIBS) $(OS_SPECIFIC_LIBS)
COMMONLIBSRC = $(DDSCOMMON)/dds_common.cxx
COMMONLIBOBJS = $(DDSCOMMON)/obj.$(CPUTYPE)/%.o
$(shell mkdir -p $(OBJS_DIR) $(DDSCOMMON)/obj.$(CPUTYPE))
default: ${IDL_TYPES} $(GENERATED_OBJS)
$(OBJS_DIR)/%.o : %.cxx %.h $(DDSCOMMON)/dds_common.h
$(CPP_COMPILER) -o $# $(DEFINES) $(INCLUDES) $(CXXFLAGS) -c $<
%.idl:
#echo "Generating CXX from $# ..." $(GENERATED_OBJS); \
$(NDDSHOME)/scripts/rtiddsgen ${IDL_DIR}/$# -d $(DDS_OUT_DIR) -I ${IDL_DIR} -replace -language C++;
if I just do this:
make
The %.idl target is called fine, when that finishes I get this output:
Generating CXX from common.idl ... obj.Linux-i686/commonSupport.o obj.Linux-i686/commonPlugin.o obj.Linux-i686/common.o
Running rtiddsgen version 4.5d, please wait ...
Done
make: *** No rule to make target `obj.Linux-i686/commonSupport.o', needed by `default'. Stop.
But then when I re-run it and everything compiles, so it works fine...
Why is this not working in one step?
commonSupport.cxx seems to depend on common.idl. Tell this to make.
commonSupport.cxx: common.idl
#echo "Generating CXX from $# ..." $(GENERATED_OBJS); \
$(NDDSHOME)/scripts/rtiddsgen ${IDL_DIR}/$# -d $(DDS_OUT_DIR) -I ${IDL_DIR} -replace -language C++;
Or, to ensure all dependencies are right:
$(GENERATED_SOURCES): common.idl
.... steps to make GENERATED_SOURCES from common.idl

Resources