How can I give the sourcefilepaths to gcc? - c

How can I give my sourcepaths to gcc?
I have my .c files in source and test directory.
How can I give gcc the path to it? Im compiling with amakefile` and always get the message
"no such file or directory test.c"
My directory structure:
make-directory|
|
|--source
|
|--Header
|
|--test
|
|--out
|
as asked:
# makefile to generate UNIT-tests
# define any directories containing header files other than /usr/include
# TODO
HEADERS = :../CUnit/headers \
CUnit/sources/Automated \
CUnit/sources/Basic \
CUnit/sources/Console \
CUnit/sources/Curses \
CUnit/sources/Framework \
CUnit/sources/Test \
CUnit/sources/Win \
CUnit/sources/wxWidget \
stub \
../source \
test
SOURCES = CUnit/headers \
CUnit/sources/Automated \
CUnit/sources/Basic \
CUnit/sources/Console \
CUnit/sources/Curses \
CUnit/sources/Framework \
CUnit/sources/Test \
CUnit/sources/Win \
CUnit/sources/wxWidget \
stub \
source \
test
# define any libraries to link into executable:
# if I want to link in libraries (libx.so or libx.a) I use the -llibname
# option, something like (this will link in libmylib.so and libm.so:
LIBS =
# TODO define the C source files
TST_SRCS = min.c max.c
SRCS = CUnit.c Automated.c Basic.c Console.c CUCurses.c CUError.c Cunit_intl.c \
MyMem.c TestDB.c TestRun.c Util.c wxWidget.c \
$(TST_SRCS)
# define the C object files
#
# This uses Suffix Replacement within a macro:
# $(name:string1=string2)
# For each word in 'name' replace 'string1' with 'string2'
#OBJ = $(SRCS:%.c=%.o)
OBJ = $(TST_SRCS:%.c=%.o)
#OBJ=$(join ($(SOURCES)), $(notdir $(SRCS:%.c=%.o)))
# define the C compiler to use
CC = gcc
# define any compile-time flags
CFLAGS = -O0 -g -Wall -fmessage-length=0 -fprofile-arcs -ftest-coverage
#TODO Linkerflags
LFLAGS = --coverage
VPATH=source
# define the executable file,
TARGET = CUnit
all:
$(CC) -I $(HEADERS) $(CFLAGS) $(OBJ) -o $(TARGET) $(LFLAGS)

You can make use of vpath or VPATH in makefile to point to the directory containing the source files.
See the online gnu make manual here.

Related

how to get all the headerfiles which are used in compiling?

I am compiling my SW using Clang (android30) and as my SW is really big, I want to extract only the files which are necessary to compile, so that I can have a smaller SW.
is there anyway to have a list of all the dependencies (header-files and .c file)?
I am using make framework to compie which looks like this:
P
PRJ_ANDROID_COMPILE := $(XXX)/aarch64-linux-android30-clang
PRJ_ANDROID_COMPILE_COMMAND = $(X) \
$(addprefix -D,$(XX)) \
$(addprefix -I ,$(XXXX) $(XXXXX)) \
$(addprefix -isystem ,$(PRJ_ANDROID_SYSTEM_INCLUDES)) \
$(addprefix -include ,$(PRJ_ANDROID_HEADER_INCLUDES)) \
-c $(abspath $<) \
-o $(XXXXX)/$(notdir$*)$(FRW_PROCESSOR_OBJ_EXT);

How can I modify my makefile to allow me to include<pthread.h>?

I am modifying an example btstack program in C. Btstack includes a makefile for porting to the raspberry pi. I need to have a timer running in the background while other things are running so I am attempting to tinker with pthreads. I've installed the library with "sudo apt-get ...." and included it in my .c file. When I attempt to compile, I get the following error (although unlike other errors it is in grey text and not red):
/usr/bin/ld: le_data_channel_server.o: undefined reference to symbol 'pthread_create##GLIBC_2.4'
/usr/bin/ld: //lib/arm-linux-gnueabihf/libpthread.so.0: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
make: *** [../../example/Makefile.inc:502: le_data_channel_server] Error 1
Here are the contents of the makefile as is:
# Makefile for Raspberry Pi
BTSTACK_ROOT ?= ../..
CORE += \
btstack_chipset_bcm.c \
btstack_chipset_bcm_download_firmware.c \
btstack_control_raspi.c \
btstack_link_key_db_tlv.c \
btstack_run_loop_posix.c \
btstack_tlv_posix.c \
btstack_uart_posix.c \
btstack_slip.c \
hci_dump_posix_fs.c \
hci_transport_h4.c \
hci_transport_h5.c \
le_device_db_tlv.c \
main.c \
wav_util.c \
btstack_stdin_posix.c \
raspi_get_model.c \
rijndael.c
# examples
include ${BTSTACK_ROOT}/example/Makefile.inc
# use (cross)compiler for Raspi
CC = arm-linux-gnueabihf-gcc
CFLAGS += -g -Wall -Werror \
-I$(BTSTACK_ROOT)/platform/embedded \
-I$(BTSTACK_ROOT)/platform/posix \
-I$(BTSTACK_ROOT)/chipset/bcm \
-I${BTSTACK_ROOT}/3rd-party/tinydir \
-I${BTSTACK_ROOT}/3rd-party/rijndael
# add 'real time' lib for clock_gettime
LDFLAGS += -lrt
VPATH += ${BTSTACK_ROOT}/3rd-party/rijndael
VPATH += ${BTSTACK_ROOT}/platform/posix
VPATH += ${BTSTACK_ROOT}/platform/embedded
VPATH += ${BTSTACK_ROOT}/chipset/bcm
EXAMPLES = ${EXAMPLES_GENERAL} ${EXAMPLES_CLASSIC_ONLY} ${EXAMPLES_LE_ONLY} ${EXAMPLES_DUAL_MODE}
EXAMPLES += pan_lwip_http_server
# use pkg-config for portaudio
# CFLAGS += $(shell pkg-config portaudio-2.0 --cflags) -DHAVE_PORTAUDIO
# LDFLAGS += $(shell pkg-config portaudio-2.0 --libs)
# hard coded flags for portaudio in /usr/local/lib
# CFLAGS += -I/usr/local/include -DHAVE_PORTAUDIO
# LDFLAGS += -L/sw/lib -lportaudio -Wl,-framework,CoreAudio -Wl,-framework,AudioToolbox -Wl,-framework,AudioUnit -Wl,-framework,Carbon
all: ${EXAMPLES}
Basically, what should I modify/add in the makefile that would allow me to compile the program in the rpi command line with a "make" command?
Edit: MFisherKDX's solution worked. I added
LDFLAGS += -lpthread to the makefile.

fatal error: json-c/json.h: No such file or directory Cross compiling on ubuntu x64 for raspberry pi arm

I am trying to compile my code on my Ubuntu x64 laptop for the Raspberry Pi.
I am able to compile and run the code on Ubuntu laptop without any issue. However when I try to compile it for Raspberry Pi I get the following error:
$make
/home/nmohan/Development/arm-bcm2708-linux-gnueabi/bin/arm-bcm2708-linux-gnueabi-gcc -g -Wall -Wextra -I /home/nmohan/github/NiRobot/inc -fPIC -shared -L/home/nmohan/github/NiRobot/Obj_arm -Wl,-rpath=/home/nmohan/github/NiRobot/Obj -o /home/nmohan/github/NiRobot/Obj_arm/libRSXA.so /home/nmohan/github/NiRobot/lib/RSXA.c -lNMT_stdlib -lNMT_log -ljson-c -lc -Ljson-c
/home/nmohan/github/NiRobot/lib/RSXA.c:11:25: fatal error: json-c/json.h: No such file or directory
compilation terminated.
Makefile:101: recipe for target '/home/nmohan/github/NiRobot/Obj_arm/libRSXA.so' failed
make: *** [/home/nmohan/github/NiRobot/Obj_arm/libRSXA.so] Error 1
#---------------------------------------#
# #
# Global Varibles #
# #
#---------------------------------------#
PROJ_DIR = /home/nmohan/github/NiRobot
BIN_DIR = $(PROJ_DIR)/bin
OBJ_DIR = $(PROJ_DIR)/Obj
OBJ_DIR_ARM = $(PROJ_DIR)/Obj_arm
INC_DIR = $(PROJ_DIR)/inc
LIB_DIR = $(PROJ_DIR)/lib
OUT_DIR = $(PROJ_DIR)/bld
OUT_DIR_ARM = $(PROJ_DIR)/bld_arm
CFLAGS = -g -Wall -Wextra -I $(INC_DIR)
SFLAGS = -fPIC -shared
RPATH = -L$(OBJ_DIR) -Wl,-rpath=$(OBJ_DIR)
RPATH_ARM = -L$(OBJ_DIR_ARM) -Wl,-rpath=$(OBJ_DIR)
GCC_DIR = /home/nmohan/Development/arm-bcm2708-linux-gnueabi/bin/arm-bcm2708-linux-gnueabi-gcc
#---------------------------------------#
# #
# Targets #
# #
#---------------------------------------#
OBJS = NMT_stdlib.so \
NMT_log.so \
RSXA.so \
PCA9685.so \
MTDR.so \
CAM_MOTOR_CTRL.so \
HCxSR04.so
#---------------------------------------#
# #
# Dependancies #
# #
#---------------------------------------#
NMT_STDLIB_LIBS = -lc
NMT_LOG_LIBS = -lc \
-lNMT_stdlib \
RSXA_LIBS = -lNMT_stdlib \
-lNMT_log \
-ljson-c \
-lc
PCA9685_LIBS = -lNMT_stdlib \
-lNMT_log \
-lc \
-lwiringPi \
-lcrypt \
-lm \
-lrt \
-lRSXA
MTDR_LIBS = -lNMT_stdlib \
-lNMT_log \
-lwiringPi \
-lPCA9685 \
-lcrypt \
-lm \
-lrt \
-lRSXA
CAM_MOTOR_CTRL_LIBS = -lNMT_stdlib \
-lNMT_log \
-lwiringPi \
-lMTDR \
-lPCA9685 \
-lcrypt \
-lm \
-lrt \
-lRSXA
HCxSR04_LIBS = -lNMT_log \
-lNMT_stdlib \
-lRSXA \
-lwiringPi \
-lcrypt \
-lm \
-lrt
TARGET_OBJS := $(foreach OBJ,$(OBJS),$(OBJ_DIR)/lib$(OBJ))
TARGET_OBJS_ARM := $(foreach OBJ,$(OBJS),$(OBJ_DIR_ARM)/lib$(OBJ))
all: $(TARGET_OBJS) \
$(TARGET_OBJS_ARM)
.PHONY: all
$(OBJ_DIR)/lib%.so: $(LIB_DIR)/%.c $(INC_DIR)/%.h
gcc $(CFLAGS) $(SFLAGS) $(RPATH) -o $# $< $($(*)_LIBS)
$(OBJ_DIR_ARM)/lib%.so: $(LIB_DIR)/%.c $(INC_DIR)/%.h
$(GCC_DIR) $(CFLAGS) $(SFLAGS) $(RPATH_ARM) -o $# $< $($(*)_LIBS)
$(OBJ_DIR)/lib%.so: $(LIB_DIR)/%.cpp $(INC_DIR)/%.hpp
g++ $(CFLAGS) $(SFLAGS) $(RPATH) -o $# $< $($(*)_LIBS)
It depends on json-c/json.h. You need to include these files that were not found.
Installing libjson-c-dev package.
sudo apt install libjson-c-dev
I've had a similar problem. It's because of the json.h which is not part of the, in my case, gcc-arm-10-3-2017.07...-aarch64-none-linux-gnu.
I simply cloned json-c git and compiled it with the cross-compiler, too. Then you can reference to the output, or you can inlcude the whole git into your project and compile it all in one.

skipping incompatible when searching for -lgpstk

I am trying "make" command on my linux to create an exe file, but I keep getting an error
/usr/bin/ld: skipping incompatible ../../../shared/gpstk/src/libgpstk.a when searching for -lgpstk
/usr/bin/ld: cannot find -lgpstk
collect2: error: ld returned 1 exit status
../../../shared/makehome/common.makefile:79: recipe for target '../../../shared/binflate/src/binflate' failed
I have tried to download libraries through search online but I can't seem to get past this error. Can someone please help me out.
Here is the makefile code:
TARGET = linux
#----------------------------------------------------------------- Directories
# Where make will put outputs and intermediate files
SVNROOTDIR = ../../..
OBJDIR = $(SVNROOTDIR)/obj
BINDIR = $(SVNROOTDIR)/shared/binflate/src
LIBDIR = $(BINDIR)
#--------------------------------------------------------------------- Outputs
# What we are trying to build:
# Note: If you want this makefile to build an EXECUTABLE, leave LIBRARY
# undefined, and vice versa if you want to build a static LIBRARY
EXECUTABLE = $(BINDIR)/binflate
LIBRARY =
#--------------------------------------------------------------------- Sources
SOURCES = $(SVNROOTDIR)/shared/core/src/basetime.cpp \
$(SVNROOTDIR)/shared/core/src/parameters.cpp \
$(SVNROOTDIR)/shared/core/src/report.cpp \
$(SVNROOTDIR)/shared/core/src/datalog.cpp \
$(SVNROOTDIR)/shared/core/src/util.cpp \
$(SVNROOTDIR)/shared/core/src/serialutil.cpp \
$(SVNROOTDIR)/shared/core/src/signaltype.cpp \
$(SVNROOTDIR)/shared/core/src/channeltype.cpp \
$(SVNROOTDIR)/shared/core/src/gridframework.cpp \
$(SVNROOTDIR)/shared/core/src/binhelper.cpp \
$(SVNROOTDIR)/shared/core/src/observables.cpp \
$(SVNROOTDIR)/shared/core/src/txid.cpp \
$(SVNROOTDIR)/shared/core/src/ReportWrapper.cpp \
$(SVNROOTDIR)/shared/binflate/src/binflate.cpp
# include directories in gcc format
INCL = -I../include \
-I$(SVNROOTDIR)/shared/core/include \
-I$(SVNROOTDIR)/shared/gpstk/include
# Libraries
LIBS = -lm -lgpstk
# Library include directories in gcc format (-Lpath/to/dir1 -Lpath/to/dir2)
INCLLIB = -L$(SVNROOTDIR)/shared/gpstk/src \
-L/usr/local/lib
# Predefine macros
PREDEFINES =
#------------------------------------ Compilers, linkers, etc. and their flags
CC = g++
CCFLAGS = -g -Wall -Wno-unknown-pragmas \
-Wno-uninitialized $(PREDEFINES)
CCINCL = $(INCL)
LIBEXE = ar
LIBFLAGS = rcs
LIBINCL =
LNKEXE = g++
LNKFLAGS =
LNKINCL =
MD = g++
MDFLAGS = -MM
MDINCL = $(INCL)
#---------------------------------------------------------- Included Makefiles
include $(SVNROOTDIR)/shared/makehome/common.makefile

Automake + libtool: pattern rule for per-object CFLAGS?

I'm using GNU Automake and libtool to compile my program. My Makefile.am looks like this:
lib_LTLIBRARIES = \
libObjectively.la
libObjectively_la_SOURCES = \
Array.c \
Class.c \
Condition.c \
Date.c \
DateFormatter.c \
Dictionary.c \
Lock.c \
Log.c \
Object.c \
String.c \
Thread.c
libObjectively_la_CFLAGS = \
-I ..
libObjectively_la_LDFLAGS = \
-pthread \
-shared
Everything compiles just fine. However, I would like to set CFLAGS for each source file using a pattern rule as well. In regular old Makefile syntax, this would look something like:
%.o: %.c
$(CC) $(CFLAGS) -D__Class=$(subst .o,,$#) -o $# $<
Is there a way to do this with Automake + libtool?
Turns out, there is no portable way to do this sort of thing.

Resources