How to use g++ without Makefile to build cpp that makes calls to other header files? - g++

I am trying to build at the command prompt the following:
g++ -c ../../TRUNK/StringUtl.cpp -o StringUtl.o // ok
g++ -c ../../TRUNK/CircularBuf.cpp -o CircularBuf.o // ok
g++ -c ../../TRUNK/UartComm.cpp -o UartComm.o // fatal error: CircularBuf.h: No such file or directory THE header to his cpp file has #include "CircularBuf.h"
UartComm.h: contents at the top of file
//---------------------------------------------------------------------------------------
// General inclusions
//---------------------------------------------------------------------------------------
#include <stdio.h>
#include "CircularBufClass.h"

This error is beacuse g++ is not able to find the header files. You would need to provide path of your header files using -I<Path> option.
Eg:
g++ -Iyourheaderfilepath/ -c ../../TRUNK/UartComm.cpp -o UartComm.o

Related

RISCV C to hex Compilation for UART

I am trying to convert UART loopback program to corresponding hex code. The command
riscv32-unknown-elf-gcc test.c -march=rv32im
riscv32-unknown-elf-gcc -o test test.c
both the command giving error
test.c:2:10: fatal error: rt/rt_api.h: No such file or directory
#include <rt/rt_api.h>
^~~~~~~~~~~~~
compilation terminated.
what is the exact command for compiling the c code with APIs, I am using Pulppissimo toolchain..
my program is
https://github.com/pulp-platform/pulp-rt-examples/tree/master/periph/uart/loopback
You have this error because rt/rt_api.h is not present in your riscv gcc search path.
To see the folders present in search path you can add -v option to your command or use:
riscv32-unknown-elf-cpp -v /dev/null /dev/null
What you can do is find the location of rt/rt_api.h and giving the path you find to gcc. Your command will be:
riscv32-unknown-elf-gcc -o test test.c -Ipath

Error in including homemade Fortran modules and libraries in Makefile

I am trying to build a very simple Makefile, that intends to use a homemade library (libf904QC.a) made of Fortran modules. The library is in /usr/local/lib64 whereas the corresponding .mod files are in /usr/local/include/f904QC
Here is the Makefile
# Makefile
NAME=NPManip
FFLAGS= -ffpe-trap=overflow -c -O3
LFLAGS=
PATH2LIB=/usr/local/lib64/
INCLUDEDIR=/usr/local/include/f904QC/
#
LIB=-L$(PATH2LIB) -I$(INCLUDEDIR) -lf904QC.a
OBJS = \
tools_NPManip.o\
NPManip.o
%.o: %.f90
gfortran $(LIB) $(FFLAGS) $*.f90
NPM: $(OBJS)
gfortran $(LFLAGS) $(OBJS) $(LIB) -o $(NAME)
clean:
#if test -e $$HOME/bin/$(NAME); then \
rm $$HOME/bin/$(NAME); \
fi
rm *.o *.mod
mrproper: clean
rm $(NAME)
install:
ln -s $(shell pwd)/$(NAME) $$HOME/bin/.
I get the following error message :
gfortran tools_NPManip.o NPManip.o -L/usr/local/lib64/ -I/usr/local/include/f904QC/ -lf904QC.a -o NPManip
/usr/lib64/gcc/x86_64-suse-linux/4.7/../../../../x86_64-suse-linux/bin/ld: cannot find -lf904QC.a
collect2: error: ld returned 1 exit status
make: * [NPM] Erreur 1
Where is the mistake? It is not obvious to me since libf904QC.o is actually located in /usr/local/lib64, which is defined by the -L option.
Thnak you for your help
You should specify either the full path to the library /usr/local/lib64/libf904QC.a or alternatively -L/usr/local/lib64 -lf90QC, without the .a in that case. From man ld:
-l namespec
--library=namespec
Add the archive or object file specified by namespec to the list of files to link. This option may be used any number of
times. If namespec is of the form :filename, ld will search the library path for a file called filename, otherwise it
will search the library path for a file called libnamespec.a.
-L searchdir
--library-path=searchdir
Add path searchdir to the list of paths that ld will search for archive libraries and ld control scripts. You may use
this option any number of times. The directories are searched in the order in which they are specified on the command
line. Directories specified on the command line are searched before the default directories. All -L options apply to
all -l options, regardless of the order in which the options appear. -L options do not affect how ld searches for a
linker script unless -T option is specified.

how to compile multi-folder Fortran Project having interfaces, modules and subroutines

I am new to Fortran. I am working on a research project where I am using an open source project that has several files distributed in multiple folders. i found the dependency of each programs but could not figure out how to compile them.
I have source code distributed in three folders.
a)modules
b)interfaces
c)subroutines
I would like to run a program named as 'Main.f90' in subroutines folder, this program has dependency of source codes from modules and interfaces folders.
I am using eclipse for folder structure and makefile to compile.
Any help with this is appreciated.
UPDATE:
I followed the answer posted by #MBR and #Stefan, for some reason VPATH did not able to find programs in source code so I explicitly gave path to those source folder in my Makefile. below is my make file script.
.PHONY: Mopac_exe clean
# Change this line if you are using a different Fortran compiler
FORTRAN_COMPILER = gfortran
SRC = src
#make main program
Mopac_exe: subroutines mopac.o
$(FORTRAN_COMPILER) mopac.o *.o -O2 -g -o bin/Mopac_exe -I Modules/
#compile all the subroutines
subroutines: interfaces
$(FORTRAN_COMPILER) -c $(SRC)/subroutines/*.F90 -J Modules/Subroutines/ -I Modules/
#compiles all the interfaces
interfaces: modules
$(FORTRAN_COMPILER) -c $(SRC)/interfaces/*.f90 -J Modules/
# build all the modules and generate .mod file in Modules directory
modules: build_vast_kind
$(FORTRAN_COMPILER) -c $(SRC)/modules/*.f90 -J Modules/
$(FORTRAN_COMPILER) -c $(SRC)/modules/*.F90 -J Modules/
# compile vastkind.f90 files and generates the .mod file in Modules directory.Every other Modules and interfaces are dependent on this.
build_vast_kind:clean
$(FORTRAN_COMPILER) -c $(SRC)/modules/vastkind.f90 -J Modules/
clean:
rm -f bin/Mopac_exe *.mod
rm -f Modules/*.mod
rm -f *.o
I compiled all the modules and placed in Modules directory of root Folder.
All compilation goes well. I get error when I build the executable. I get following error.
gfortran mopac.o *.o -O2 -g -o bin/Mopac_exe -I Modules/
mopac.o: In function `main':
mopac.F90:(.text+0x27c1): multiple definition of `main'
mopac.o:mopac.F90:(.text+0x27c1): first defined here
getdat.o: In function `getdat_':
getdat.F90:(.text+0x22): undefined reference to `iargc_'
getdat.F90:(.text+0xf2): undefined reference to `getarg_'
symr.o: In function `symr_':
symr.F90:(.text+0xd3f): undefined reference to `symp_'
writmo.o: In function `writmo_':
writmo.F90:(.text+0x20c2): undefined reference to `volume_'
collect2: error: ld returned 1 exit status
make: *** [Mopac_exe] Error 1
`iargc_' is being used in 'getdat file and iargc is already compiled. why there is error while making the executable saying undefined reference? what am I missing?
You can do a Makefile which looks like that
F90=gfortran
FFLAGS = -O0
VPATH = modules:interfaces:subroutines:
MODOBJ = module1.o module2.o ...
your_executable: $(MODOBJ) main.o
$(F90) main.o -o your_executable
%.o:%.f90
$(F90) $(FFLAGS) -c $^ -o $#
VPATH is the paths of the directories where your Makefile will look for source files, so if you compile your source in the root directory of modules/, interfaces/ and subroutines/, you just have to set up VPATH like that.
If you have many objects and you don't want to write everything by hand, you can retrieve them using the following trick
F90 = gfortran
FFLAGS = -O0
VPATH = modules:interfaces:subroutines
SRCOBJ = $(wildcard modules/*f90)
MODOBJ = $(SRCOBJ:.f90=.o)
your_executable: $(MODOBJ) main.o
$(F90) main.o -o your_executable
%.o:%.f90
$(F90) $(FFLAGS) -c $^ -o $#
The wildcard command in a Makefile allows you to use a joker *; then you just have to say that in the strings you will retrieve in $(SRCOBJ), you want to substitute .f90 by .o to get the filenames of your modules.
You can create your Makefiles as usual. The biggest problem should be the .mod files. The easiest solution to this problem is to create a separate folder, where these files are stored and searched for.
This can be achieved with the -J and the -module flags for gfortran and ifort, respectively.

G++ -I option for compiling program

Here is a little problem that cannot be resolved by me such a Linux program newbie.
Now I have a main.cpp program which need to be compiled, there is a
#include "Down.h"
in the front of file.
Actually, this header file exist in the other directory, which locates at
../../../include
directory. Besides, some other header files needed by Down.h also locate at this ../../../include directory.
Here is the problem, I compile main.cpp with command
g++ -I /../../../include main.cpp
However, it gives lots of error info which means it is not correct to be done like this.
Should I also change the include declaration into this one?
#include "../../../include/DownConvert.h"
May you please leave me with some advice? Thanks.
Edit:
After using g++ -I ../../../include main.cpp, I get the following errors:
$ g++ -I ../../../include main.cpp
In file included from ../../../include/DownConvert.h:98,
from main.cpp:92: ../../../include/ResizeParameters.h:4:22: error:
TypeDefs.h: No such file or directory
In file included from /usr/include/c++/4.4/bits/stl_algo.h:61,
from /usr/include/c++/4.4/algorithm:62,
from ../../../include/H2
g++ -I /../../../include main.cpp
See that leading slash after the -I? That's an absolute path.
Change it to a relative path (shown below) and it'll work OK.
g++ -I ../../../include main.cpp
g++ -I ../../../include main.cpp
ought to work
Try to use -v option:
g++ -v -I ../../../include main.cpp
And check that list of directories to search for include files contains your folder and there is no complains that this folder is absent. If there is this sort of complains correct the path that you give after -I

how do I get CLSQL to look for mysql.h in non-standard directory?

in the error log:
CLSQL is doing:
gcc -I /usr/local/include/mysql -I /usr/include/mysql -I /sw/include/mysql -I /opt/local/include/mysql -I /usr/local/mysql/include -fPIC -c clsql_mysql.c -o clsql_mysql.o
and gets error:
clsql_mysql.c:34:19: mysql.h: No such file or directory
and a bunch of C errors because it doesn't include a header.
I want it to do:
gcc -I /usr/local/include/mysql -I /usr/include/mysql -I /sw/include/mysql -I /opt/local/include/mysql -I /usr/local/mysql/include <b>-I /usr/local/mysql/include/mysql</b> -fPIC -c clsql_mysql.c -o clsql_mysql.o
I tried running: (clsql:push-library-path #P"/usr/local/mysql/include/mysql/") already. Didn't work.
HALP!
check that file exists and no permissions problem by inserting #include with full name at the beginning of clsql_mysql.c file:
#include "/usr/local/mysql/include/mysql/mysql.h"
if compiles well - undo changes and resolve path provided by -I parameter
if doesn't compile and says "file does'nt exist" - check that file exists and no permission problems
I'm not familiar with CLSQL specifically, but if it uses cffi-grovel to generate that command, then prior to loading it you should try adding your "-I/usr/local/mysql/include/mysql/" to the cffi-grovel::*cc-flags* list. You could also add it in the asd file, as the grovel-file asdf component takes an optional cc-flags argument.