Native and cross compile using GNU make, embedded C - c

I'm working on a Makefile to make me able to native and cross-compile. choosing wether to compile for host linux or ti MSP432 should be done from command line:
$ make build PLATFORM=MSP432
$ make build PLATFORM=HOST
here's my Makefile that I tried to do it in:
include sources.mk
ifeq ($(PLATFORM),MSP432)
# Platform Overrides
# Architectures Specific Flags
LINKER_FILE = msp432p401r.lds
CPU = cortex-m4
ARCH = thumb
SPECS = nosys.specs
# Compiler Flags and Defines
CC = arm-none-eabi-gcc
LD = arm-none-eabi-ld
endif
ifeq ($(PLATFORM),HOST)
CC = gcc
endif
TARGET = c1m1
LDFLAGS = -Wl,-Map=$(TARGET).map -T $(LINKER_FILE)
CFLAGS = -mcpu=$(CPU) -m$(ARCH) --specs=$(SPECS) -Wall -Werror -g -O0
-std=c99
CPPFLAGs =
.PHONY: build
build: $(TARGET).out
.PHONY: clean
clean:
rm -f $(OBJS) $(TARGET).out $(TARGET).map
%.o : %.c
$(CC) -c $< $(CFLAGS) -o $#
OBJS = $(SOURCES:.c=.o)
$(TARGET).out: $(OBJS)
$(CC) $(OBJS) $(CFLAGS) $(LDFLAGS) -o $#
is this the right way to do that?
there's also another weird error happening when I compile using:
$ make main.o PLATFORM=MSP432
I get this error:
arm-none-eabi-gcc -c main.c -mcpu=cortex-m4 -mthumb --
specs=nosys.specs -Wall -Werror -g -O0 -std=c99 -o main.o
main.c:23:22: fatal error: platform.h: No such file or directory
#include "platform.h"
^
compilation terminated.
Makefile:64: recipe for target 'main.o' failed
make: *** [main.o] Error 1
and when I compile using this:
$ make main.o PLATFORM=HOST
I get this error, they are 2 different errors and I can't understand the reason behind this.
gcc -c main.c -mcpu= -m --specs= -Wall -Werror -g -O0 -std=c99 -o
main.o
gcc: warning: ‘-mcpu=’ is deprecated; use ‘-mtune=’ or ‘-march=’
instead
gcc: error: missing argument to ‘-mcpu=’
gcc: error: missing argument to ‘--specs=’
gcc: error: unrecognized command line option ‘-m’
Makefile:64: recipe for target 'main.o' failed
make: *** [main.o] Error 1
I posted those apparently different questions in 1 question, because I think they are affecting eachother.
this is also another headerfile that is called platform.h that has some conditionals to include some directives, which after the answer I think might be needed for compile time switches
#ifndef __PLATFORM_H__
#define __PLATFORM_H__
#if defined (MSP432)
#include "msp432p401r.h"
#define PRINTF(...)
#elif defined (HOST)
#include <stdio.h>
#define PRINTF(...) printf(__VA_ARGS__)
#else
#error "Platform provided is not supported in this Build System"
#endif
#endif /* __PLATFORM_H__ */

First, I will answer the case when PLATFORM and HOST are the same:
$ make main.o PLATFORM=HOST
I get this error, they are 2 different errors and I can't understand the reason behind this.
gcc -c main.c -mcpu= -m --specs= -Wall -Werror -g -O0 -std=c99 -o
main.o
gcc: warning: ‘-mcpu=’ is deprecated; use ‘-mtune=’ or ‘-march=’
instead
gcc: error: missing argument to ‘-mcpu=’
gcc: error: missing argument to ‘--specs=’
gcc: error: unrecognized command line option ‘-m’
Makefile:64: recipe for target 'main.o' failed
make: *** [main.o] Error 1
This is due to your makefile: CPU, ARCH and SPECS are only set when
PLATFORM is MSP432
So the line CFLAGS = -mcpu=$(CPU) -m$(ARCH) --specs=$(SPECS) -Wall -Werror -g -O0 -std=c99
is evalued as CFLAGS = -mcpu= -m --specs= -Wall -Werror -g -O0 -std=c99
When gcc is invoked with CFLAGS as argument, which is incorrect.
To correct this, you can make theses little changes in your makefile:
include sources.mk
ifeq ($(PLATFORM),MSP432)
# Platform Overrides
# Architectures Specific Flags
LINKER_FILE = msp432p401r.lds
CPU = cortex-m4
ARCH = thumb
SPECS = nosys.specs
LDFLAGS_ARCH = -T $(LINKER_FILE)
CFLAGS_ARCH = -mcpu=$(CPU) -m$(ARCH) --specs=$(SPECS)
# Compiler Flags and Defines
CC = arm-none-eabi-gcc
LD = arm-none-eabi-ld
endif
ifeq ($(PLATFORM),HOST)
CC = gcc
endif
TARGET = c1m1
LDFLAGS = -Wl,-Map=$(TARGET).map $(LDFLAGS_ARCH)
CFLAGS = $(CFLAGS_ARCH) -Wall -Werror -g -O0
-std=c99
CPPFLAGs =
Now, for the main.c:23:22: fatal error: platform.h: No such file or directory
You have to find where this file is locatted and eventually add it as a gcc option.
For instance, if the file platform.h is in /some/directory, you can add this
option to gcc to help it to find it:
-I/some/directory
So in makefile, you can have this line:
CFLAGS_ARCH = -mcpu=$(CPU) -m$(ARCH) --specs=$(SPECS) -I/some/directory
EDIT
In comments, you add this problem for your question:
that solved it the errors are consistent now, and here it is
In file included from main.c:23:0: ./include/common/platform.h:30:2: error:
#error "Platform provided is not supported in this Build System" #error "... *** [main.o] Error 1
Regarding the platform.h file, macro MSP432 or HOST must be defined in order to run.
To define such macro, the -D option must be passed to gcc.
So the idea is to add some line to the makefile to define MSP432 or HOST when necessary:
...
ifeq ($(PLATFORM),MSP432)
# Platform Overrides
# Architectures Specific Flags
LINKER_FILE = msp432p401r.lds
CPU = cortex-m4
ARCH = thumb
SPECS = nosys.specs
LDFLAGS_ARCH = -T $(LINKER_FILE)
CFLAGS_ARCH = -mcpu=$(CPU) -m$(ARCH) --specs=$(SPECS) -DMSP432
# Compiler Flags and Defines
CC = arm-none-eabi-gcc
LD = arm-none-eabi-ld
endif
ifeq ($(PLATFORM),HOST)
CFLAGS_ARCH = -DHOST
CC = gcc
endif
...

Related

Fatal error when trying to set up fftw3 with c, MacOS Monterey

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!

x86_64-conda_cos6-linux-gnu-cc: Command not found with postgres function makefile

I am trying to do this tutorial: https://linuxgazette.net/139/peterson.html so I can learn to write postgres functions.
It says to use this makefile, which fails:
MODULES = example
PGXS := $(shell pg_config --pgxs)
include $(PGXS)
Errors:
(base) weather#weather:~/pg_func_learn$ make
/tmp/build/80754af9/postgresql-split_1552510884761/_build_env/bin/x86_64-conda_cos6-linux-gnu-cc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe -I/home/weather/anaconda3/include -fdebug-prefix-map=/tmp/build/80754af9/postgresql-split_1552510884761/work=/usr/local/src/conda/postgresql-split-11.2 -fdebug-prefix-map=/home/weather/anaconda3=/usr/local/src/conda-prefix -fPIC -I. -I./ -I/home/weather/anaconda3/include/server -I/home/weather/anaconda3/include/internal -DNDEBUG -D_FORTIFY_SOURCE=2 -O2 -D_GNU_SOURCE -I/home/weather/anaconda3/include -c -o example.o example.c
make: /tmp/build/80754af9/postgresql-split_1552510884761/_build_env/bin/x86_64-conda_cos6-linux-gnu-cc: Command not found
<builtin>: recipe for target 'example.o' failed
make: *** [example.o] Error 127
I have tried several things, including updating conda and anaconda, as per the answers to similar questions. Nothing has worked yet.
How do I compile postgres function given this situation?
This error has occurred caused by absent GCC compiler((/tmp/build/80754af9/postgresql-split_1552510884761/_build_env/bin/x86_64-conda_cos6-linux-gnu-cc) in your machine. Therefore, you need to rewrite a make rule.
Update the gcc compiler version 6 or later.
Rewrite the Makefile by adding "CC := /usr/bin/gcc" below the line of "include $(PGXS)".
ifdef USE_PGXS
PG_CONFIG = pg_config
PGXS := $(shell $(PG_CONFIG) --pgxs)
include $(PGXS)
CC := /usr/bin/gcc
3. Finally, compile your extension. (USE_PGXS=1 make)

How to add a library path to a makefile?

I was making a makefile for cross-compilation between a host and arm based microcontroller. So how do I add a specific library to the recipe of the makefile?
include sources.mk
ifeq ($(PLATFORM),MSP432)
# Platform Overrides
# Architectures Specific Flags
LINKER_FILE = msp432p401r.lds
CPU = cortex-m4
ARCH = thumb
SPECS = nosys.specs
CPP = -mcpu=$(CPU) -m$(ARCH) --specs=$(SPECS) -DMSP432
# Compiler Flags and Defines
CC = arm-none-eabi-gcc
LD = arm-none-eabi-ld
endif
ifeq ($(PLATFORM),HOST)
CC = gcc
CPP= DHOST
endif
TARGET =c1m2
LDFLAGS = -Wl,-Map=$(TARGET).map -T $(LINKER_FILE)
CFLAGS = -Wall -Werror -g -O0 -$(CPP) -I/Desktop/ese-coursera-
course1/assessments/m2/include/common/platform.h
-std=c99
.PHONY: build
build: all
.PHONY: all
all: $(TARGET).out
.PHONY: clean
clean:
rm -f $(OBJS) $(TARGET).out $(TARGET).map
%.o : %.c
$(CC) -c $< $(CFLAGS) -o $#
OBJS = $(SOURCES:.c=.o)
$(TARGET).out: $(OBJS)
$(CC) $(OBJS) $(CFLAGS) $(LDFLAGS) -o $#
Is this the right way of doing it?(all the tabs and indents for the make file has been given properly, though it is not seen in the code here)
Iam getting this as the error
main.c:23:10: fatal error: platform.h: No such file or directory
#include "platform.h"
^~~~~~~~~~~~
compilation terminated.
Makefile:67: recipe for target 'main.o' failed
make: *** [main.o] Error 1
Also this is my source.mk file
SOURCES = Desktop/ese-coursera-course1/assessments/m2/src
# Add your include paths to this variable
INCLUDES = Desktop/ese-coursera-course1/assessments/m2/include/common/platform.h
Can anyone help me out?
First, try to make sure all paths you give are relative to the Makefile.
I'm surprised your compiler actually does anything since your SOURCES variable is a directory, not a list of .c files. It should probably look like:
SOURCES = src/main.c
this will make OBJS contain src/main.o and the implicit rule will be able to fire.
Next, the INCLUDES variable should reference a directory using the -I flag, like so:
INCLUDES = -Iinclude/common
You should then use it in the CFLAGS definition as follows:
CFLAGS = -Wall -Werror -g -O0 -$(CPP) $(INCLUDES) -std=c99
Other things I noticed:
Your LDFLAGS unconditionally uses the msp432p401r.lds linker script, even in the HOST case. This should probably be added conditionally in the ifeq block like so: LDFLAGS += -T msp432p401r.lds. You should be careful not to overwrite LDFLAGS later with LDFLAGS=, so it is best if you move the definition of CFLAGS and LDFLAGS to the top of the file or always use += to modify these variables.
Following on from that: if your link step uses the linker script, you should add an explicit rule that encodes this dependency: $(TARGET).out: $(LINKER_FILE).
The build -> all -> $(TARGET).out dependency chain is a bit redundant. Why not make build directly depend on $(TARGET).out?

GNU make doesn't include headerfile

I have been searching for 6 hours and I can't seem to find the issue with this GNU make file, everytime I try to compile by main.o by the order
make main.o
it gives me this error:
arm-none-eabi-gcc -c main.c -mcpu=cortex-m4 -mthumb --
specs=nosys.specs -Wall -Werror -g -O0 -std=c99 -o main.o
main.c:23:22: fatal error: platform.h: No such file or directory
#include "platform.h"
^
compilation terminated.
Makefile:52: recipe for target 'main.o' failed
make: *** [main.o] Error 1
makefile:
include sources.mk
# Platform Overrides
PLATFORM = MSP432
# Architectures Specific Flags
LINKER_FILE = msp432p401r.lds
CPU = cortex-m4
ARCH = thumb
SPECS = nosys.specs
# Compiler Flags and Defines
CC = arm-none-eabi-gcc
LD = arm-none-eabi-ld
TARGET = c1m1
LDFLAGS = -Wl,-Map=$(TARGET).map -T $(LINKER_FILE)
CFLAGS = -mcpu=$(CPU) -m$(ARCH) --specs=$(SPECS) -Wall -Werror -g -O0 -std=c99
CPPFLAGs =
.PHONY: all
all: $(TARGET).out
.PHONY: clean
clean:
rm -f $(OBJS) $(TARGET).out $(TARGET).map
%.o : %.c
$(CC) -c $< $(CFLAGS) -o $#
OBJS = $(SOURCES:.c=.o)
$(TARGET).out: $(OBJS)
$(CC) $(OBJS) $(CFLAGS) $(LDFLAGS) -o $#
sources.mk:
# Add your Source files to this variable
SOURCES =./main.c \
./memory.c \
./startup_msp432p401r_gcc.c \
./system_msp432p401r.c \
./interrupts_msp432p401r_gcc.c
# Add your include paths to this variable
INCLUDES =-I./include/CMSIS \
-I./include/common \
-I./include/msp432
here's my code on github:
github repository
You have not used the INCLUDES macro in makefile's CFLAGS macro. Consequently the arm-none-eabi-gcc ... command line does not specify the include paths to the compiler (or strictly the pre-processor).
CFLAGS = -mcpu=$(CPU) -m$(ARCH) --specs=$(SPECS) $(INCLUDES) -Wall -Werror -g -O0 -std=c99
^^^^^^^^^^^

Cross compiling a application using Shared libraries

I am having a existing application which compiles and executes in GCC.
I am cross compiling the same application, I am able to cross compile and generate the shared libraries. But when I try to use the shared libraries for linking to a application it is giving the following errors
arm-poky-linux-gnueabi-gcc MSO_Version.o MSO_Connect.o MSO_errors.o -o bin/MSO_Version_shared -Wall -g -Os -fPIC -march=armv7-a -mfloat-abi=hard -mfpu=neon -mtune=cortex-a7 --sysroot=/home/fsl-release-bsp/build_image/tmp/sysroots/imx6ulevk -I../include -I../wsq -I/home/fsl-release-bsp/build_image/tmp/sysroots/imx6ulevk/usr/include/ -L/opt/poky/1.8/sysroots/cortexa7hf-vfp-neon-poky-linux-gnueabi/usr/lib -L../lib -L/opt/poky/1.8/sysroots/i686-pokysdk-linux/lib -L/opt/poky/1.8/sysroots/i686-pokysdk-linux/usr/lib -L/home/fsl-release-bsp/build_image/tmp/sysroots/imx6ulevk/usr/lib -lMSO -lMSOComm -lusb
/opt/poky/1.8/sysroots/i686-pokysdk-linux/usr/libexec/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/4.9.2/ld: skipping incompatible /opt/poky/1.8/sysroots/i686-pokysdk-linux/lib/libgcc_s.so.1 when searching for libgcc_s.so.1
/opt/poky/1.8/sysroots/i686-pokysdk-linux/usr/libexec/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/4.9.2/ld: cannot find /lib/libc.so.6
/opt/poky/1.8/sysroots/i686-pokysdk-linux/usr/libexec/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/4.9.2/ld: cannot find /usr/lib/libc_nonshared.a
/opt/poky/1.8/sysroots/i686-pokysdk-linux/usr/libexec/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/4.9.2/ld: cannot find /lib/ld-linux-armhf.so.3
collect2: error: ld returned 1 exit status
make[1]: *** [MSO_Version_shared] Error 1
make[1]: Leaving directory `/home/linux/ESYS-IMP-LINUXAPP-SUF-24092012-V0.01/Internal Release/ESYS-IMP-LinuxApp-SUF-LIB-SRS-V0.01/samples'
make: *** [samples] Error 2
If I compile with static libraries executables are getting generated. Below is my makefile
export CROSS_COMPILE
CC = $(CROSS_COMPILE)gcc
ifeq ($(DEBUG),yes)
CFLAGS = -Wall -g -Os -fPIC -march=armv7-a -mfloat-abi=hard -mfpu=neon -mtune=cortex-a7 --sysroot=/home/fsl-release-bsp/build_image/tmp/sysroots/imx6ulevk
else
CFLAGS = -Wall -Os -fPIC -march=armv7-a -mfloat-abi=hard -mfpu=neon -mtune=cortex-a7 --sysroot=/home/fsl-release-bsp/build_image/tmp/sysroots/imx6ulevk
endif
LIB_USB = -lusb
LIB_SDL = -lSDL -lSDL_ttf
LIBPATH += -L/opt/poky/1.8/sysroots/cortexa7hf-vfp-neon-poky-linux-gnueabi/usr/lib
LIBPATH += -L../lib
LIBPATH += -L/opt/poky/1.8/sysroots/i686-pokysdk-linux/lib
LIBPATH += -L/opt/poky/1.8/sysroots/i686-pokysdk-linux/usr/lib
LIBPATH += -L/home/fsl-release-bsp/build_image/tmp/sysroots/imx6ulevk/usr/lib
LIB_STATIC_SAGEM = ../lib/libMSO.a ../lib/libMSOComm.a
LIB_SAGEM = -lMSO -lMSOComm
SRCINCLUDE += -I../include -I../wsq
SRCINCLUDE += -I/home/fsl-release-bsp/build_image/tmp/sysroots/imx6ulevk/usr/include/
EXEC_PATH = bin
EXEC_STATIC = MSO_Version #MSO_TestBio MSO_TestThread
EXEC_SHARED = MSO_Version_shared #MSO_TestBio_shared
MSO_Version_SRC = MSO_Version.c MSO_Connect.c MSO_errors.c
MSO_Version_OBJ = $(MSO_Version_SRC:.c=.o)
.PHONY: all
all: $(EXEC_STATIC) $(EXEC_SHARED)
%.o: %.c
$(CC) -c $(CFLAGS) $(SRCINCLUDE) $< -o $#
.PHONY: MSO_Version MSO_Version_shared
MSO_Version: $(MSO_Version_OBJ)
$(CC) $(MSO_Version_OBJ) -o $(EXEC_PATH)/MSO_Version $(CFLAGS) $(SRCINCLUDE) $(LIB_STATIC_SAGEM) $(LIB_USB)
MSO_Version_shared: $(MSO_Version_OBJ)
$(CC) $(MSO_Version_OBJ) -o $(EXEC_PATH)/MSO_Version_shared $(CFLAGS) $(SRCINCLUDE) $(LIBPATH) $(LIB_SAGEM) $(LIB_USB)
You can see I am generating two executables one with the static library and other with shared library.
I am using poky 1.8 and kernel is 2.6.38. Using NXP IMX6ULEVK board.
Can anyone tell me where I am going wrong
You specify architecture as armv7-a:
-march=armv7-a
but link towards i686 libraries:
-L/opt/poky/1.8/sysroots/i686-pokysdk-linux/lib -L/opt/poky/1.8/sysroots/i686-pokysdk-linux/usr/lib

Resources