I trying to compile a .cpp program from Debian 6. I have a working makefile, which only works with .c archives, but I need to compile my program on .cpp. So here its the makefile:
#
# Build the examples.
#
# The great thing about GNU Make is that it knows so much about your lifestyle, you don't need to
# tell it the obvious things. This file is a minimal makefile.
# You run this makefile by typing `make' in the directory with the makefile
# You can find out more about make by typing `info make'.
# This tells Make that we need to link everything with the robot code:
LDLIBS=/usr/lib/robot.a
CC=gcc
# This variable has a handy copy of all the things we aim to produce
EXEC= read_sensor turn_on_and_off control_joint cerrar_mano
# Thses are all the things we need to compile:
all: ${EXEC}
CFLAGS=-I/usr/realtime/include/ -D_ISOC99_SOURCE -D_GNU_SOURCE - rtai_types -rtai_lxrt
# Make now looks around the directory, finds the read_sensor.c and
# turn_on_and_off.c files, works out it can compile them into the
# read_sensor and turn_on_and_off executables, and does so!
# However, for the kernel modules, we need to do a bit more.
rt_control: rt_control.o
ld -E -O2 -r -o $# $^
# This is an example of making a binary from several object files, as an example only!
fred: bert.o jim.o
ld -E -O2 -o $# $^
# We need to tell the compiler quite a lot about where to find the right header files
rt_control.o: control.c
$(CC) -c -O2 -DREALTIME -DRTAI_3 -I./include/ -I. -I../ -isystem /usr/realtime/include/ -isystem /usr/src/linux/include -o $# $^
# We put some housekeeping in here:
# This target is used to remove intermediate files Make produces.
clean:; rm -f *.o
# This target is used to remove output files Make produces as well.
realclean: clean
rm -f ${EXEC}
So when I write on terminal make myprogramname.cpp This is what i get:
In file included from /usr/include/robot/robot.h:38,
from Programa_Hand2.cpp:4:
/usr/include/robot/sensor.h:24:44: error: rtai_types.h: No such file or directory
/usr/include/robot/sensor.h:38:23: error: rtai_lxrt.h: No such file or directory
In file included from /usr/include/robot/robot.h:38,
from Programa_Hand2.cpp:4:
/usr/include/robot/sensor.h:168: error: 'RTIME' does not name a type
In file included from /usr/include/robot/robot.h:38,
from Programa_Hand2.cpp:4:
/usr/include/robot/sensor.h:178: error: 'RTIME' does not name a type
In file included from Programa_Hand2.cpp:4:
/usr/include/robot/robot.h:194: error: 'RTIME' does not name a type
/usr/include/robot/robot.h:244: error: 'RTIME' does not name a type
Programa_Hand2.cpp: In function 'int main()':
Programa_Hand2.cpp:18: error: expected initializer before 'printf'
Programa_Hand2.cpp:19: error: 'f' was not declared in this scope
hand#hand-cartagena:~/Desktop/Joaquin/examples$ make Programa_Hand2
g++ Programa_Hand2.cpp /usr/lib/robot.a -o Programa_Hand2
In file included from /usr/include/robot/robot.h:38,
from Programa_Hand2.cpp:4:
/usr/include/robot/sensor.h:24:44: error: rtai_types.h: No such file or directory
/usr/include/robot/sensor.h:38:23: error: rtai_lxrt.h: No such file or directory
In file included from /usr/include/robot/robot.h:38,
from Programa_Hand2.cpp:4:
/usr/include/robot/sensor.h:168: error: 'RTIME' does not name a type
In file included from /usr/include/robot/robot.h:38,
from Programa_Hand2.cpp:4:
/usr/include/robot/sensor.h:178: error: 'RTIME' does not name a type
In file included from Programa_Hand2.cpp:4:
/usr/include/robot/robot.h:194: error: 'RTIME' does not name a type
/usr/include/robot/robot.h:244: error: 'RTIME' does not name a type
Programa_Hand2.cpp: In function 'int main()':
Programa_Hand2.cpp:18: error: expected initializer before 'printf'
Programa_Hand2.cpp:19: error: 'f' was not declared in this scope
make: *** [Programa_Hand2] Error 1
Also tried with gcc -o Programa_Hand2 Programa_Hand2.cpp and getting the same error, I think its something I have to change with the makefile but dont know what its wrong there
Any ideas? Im new to programming and linux, so Im getting a bit mad with this, because I need the program to be compiled on c++, and I compiled it and works properly on c. Thanks for your help!
Edit: doing some research from the internet, seems to be that RTAI doesnt support C++; so there is any way to compile it in C++? Also, my #includes are:
#include <stdio.h>
#include <robot/robot.h>
#include <unistd.h>
#include <error.h>
#include <iostream>
#include "RobotConfig.h"
Thanks again!
In short you need to use the command g++ not gcc for C++ files.
You need to compile C++ files with a C++ compiler not a C compiler and there are differences.
The command for GNU C++ Compiler is g++ assuming you have it installed. That is likely since you seem to have a C compiler.
Add a variable to your Makefile named CXX and set it equal to g++. Then compile your C++ code using the CXX variable as the command instead of the CC variable.
$(CXX) -o <.output file name> <.cpp files go here>
Related
I'm trying to use cURL but every time I try to compile a project with it, I get the same error mentioned in the title. I am aware of the dozen or so post about similar issues. However, most of the solutions I've read so far either don't work or I can't seem to find an analog for me as I'm using mingw32 on a windows 10 OS.
I am trying to use the curl-7.76.1-win32-mingw.zip. I got it from the cURL site, and no matter where I try to stick the files, I can't get anything to compile correctly.
Someone, please explain your solution or ideas to me like I'm 5. My brain has melted.
Here is the actual error:
PS C:\Users\Me> g++ -I "C:\Program Files\Curl\curl-7.76.1-win32-mingw\include\curl" -c "D:\Personal\Projects\#####\#####\#####\#####\main.cpp" -o "D:\Personal\Projects\#####\#####\#####\#####/main"
In file included fromD:\Personal\Projects\#####\#####\#####\#####\main.cpp:4:
D:\Personal\Projects\#####\#####\#####\#####\testapi.hpp:7:10: fatal error: curl/curl.h: No such file or directory
#include <curl/curl.h>
^~~~~~~~~~~~~
compilation terminated.
[Finished in 0.6s]"
Mingw is gcc for windows. Practically everything that applies to gcc applies to mingw.
It's better to use relative paths in your Makefile and with gnu make (which is part of mingw/msys) always use '/' as path separator. If you have to use absolute path like C:\dev\projects\mylib use it this way: C:/dev/projects/mylib.
Back to your curl: curl/curl.h is in C:\path\to\curl-7.76.1-win32-mingw\include\curl\curl.h so you need to add include directory to your gcc command line that points to the right directory, which is C:\path\to\curl-7.76.1-win32-mingw\include because you use "curl/curl.h". If you don't have it (that curl) installed system wide it's also better to use "curl/curl.h" in #include path than <curl/curl.h>.
So all you have to do is add -Ipath/to/curl-7.76.1-win32-mingw/include to your compile line, like:
g++ -O2 -Ipath/to/curl-7.76.1-win32-mingw/include -c -omain.o main.cpp
It can be done automatically in Makefile:
CXX = g++
CFLAGS += -O2
INCLUDES += -Ipath/to/curl-7.76.1-win32-mingw/include
.cpp.o:
$(CXX) -c $(CXXFLAGS) $(INCLUDES) $*.cpp
You write:
g++ -I "C:\Program Files\Curl\curl-7.76.1-win32-mingw\include\curl"
try better:
g++ -I "C:\Program Files\Curl\curl-7.76.1-win32-mingw\include"
as you have written in the code sample:
#include <curl/curl.h>
or conserve the -I option as it was, and change your #include to:
#include <curl.h>
I sincerely hope this will help.
I'd like to learn how to use lapack together with C/C++ code in Windows. I am a C/C++ programming newbie, so I know how to code in C, how to organize the code in h/c files, and how to compile them with gcc under cygwin / MinWG / VS. What I'm totally new to is the use of external libraries, such as lapack.
To learn how to use it with Cygwin (64bit), I followed the procedure indicated here
(http://matrixprogramming.rudnyi.ru/2011/04/using-lapack-from-c/)
which seemed to be successful and created the built version of lapack in the folder $HOME\lapack-3.3.0
Now I want to re-run this example: main.cc
What I did was to try to compile it by calling
$ g++ -O3 main.cc -L$HOME/lapack-3.3.0 -llapack -lblas -lgfortran -I ./include -o main
and what I get is
main.cc: In function ‘int main(int, char**)’:
main.cc:39:23: error: ‘dgetf2’ was not declared in this scope
info = dgetf2(A, ipvt);
^
main.cc:45:23: error: ‘dgetrf’ was not declared in this scope
info = dgetrf(A, ipvt);
^
main.cc:55:26: error: ‘dgetrs’ was not declared in this scope
info = dgetrs(A, B, ipvt);
^
where the "include" folder only contains the
Matrix.h
matrix class required in main.cc
To me it seems that some header files are missing. What I do not understand is how this is possible if the building process of the libraries was successful. Do I have to give further "-I options"? I looked for "dgetf2", "dgetrf" and "dgetrs" in the lapack-3.3.0 folder, so I found the object files in $HOME\lapack-3.3.0\SRC but if I include them as well as
$ g++ -O3 main.cc -L$HOME/lapack-3.3.0 -llapack -lblas -lgfortran -I ./include -I $HOME/lapack-3.3.0/SRC -o main
I get exactly the same error which makes somehow sense as they are object files and not header files. What am I doing wrong? How does one manage declarations of functions implemented in external libraries?
Thanks!
I’m a newbie to C++ and Linux. There is this code I’m playing with that requires me to install the HElib (Homomorphic encryption library and other libraries - GMP, NTL) which I did. I want to compile the code (main.cpp) that has a header file (FHE.h) in HElib. My problem is how can I link FHE.h (in HElib folder) and main.cpp (in another folder) together so that I can compile them. I have tried some commands
g++ -I/Home/HElib/src/FHE.h main.cpp -o main
Error message
main.cpp:1:17: fatal error: FHE.h: No such file or directory
compilation terminated.
Another command line
g++ -I/Home/HElib/Src/FHE.h -I/Home/SimpleFHESum-master/SimpleFHESum-master/main.cpp -o main]
Error Message
g++: fatal error: no input files
compilation terminated.
What's wrong and how can I fix this?
The -I flag adds the following directory to the include path of the compiler. This enables you to write e.g. #include "FHE.h" even though that file is not located in the same folder as the source file you're trying to compile.
Have you tried just removing the 'FHE.h' part from your -I directive?
g++ -I/Home/HElib/src ...
One of the header file for example BamReader.h from the bamtools package is install at
/usr/local/include/bamtools/api
I have tried to set the include flag to either:
CXXFLAGS="$CXXFLAGS -I/usr/local/include/bamtools"
or
CXXFLAGS="$CXXFLAGS -I/usr/local/include/bamtools/api"
AC_CHECK_HEADERS([BamReader.h], [bamtools_found=yes], [AC_MSG_ERROR([Unable to find bambools header])])
If I check the config.log file, the first one gave me an error:
configure:3837: g++ -c -g -O2 -I/usr/local/include/bamtools conftest.cpp >&5
conftest.cpp:54:23: fatal error: BamReader.h: No such file or directory
compilation terminated.
The second gave me the following error:
configure:3837: g++ -c -g -O2 -I/usr/local/include/bamtools/api conftest.cpp >&5
In file included from conftest.cpp:54:0:
/usr/local/include/bamtools/api/BamReader.h:13:28: fatal error: api/api_global.h: No such file or directory
#include "api/api_global.h"^M
^
compilation terminated.
configure:3837: $? = 1
The problem is with BamReader.h trying to include api/api_global.h
I got a simple fix by putting the api into the AC_CHECK_HEADERS the it worked.
CXXFLAGS="$CXXFLAGS -I/usr/local/include/bamtools"
AC_CHECK_HEADERS([api/BamReader.h], [bamtools_found=yes], [AC_MSG_ERROR([Unable to find bambools header])])
configure output
checking api/BamReader.h usability... yes
Essentially, you have to use the library by appending api/ to every header. Please comment: is this a good practice?
I have a C library made by cluster.h and cluster.c. I compiled that with gcc -c cluster.c.
I have to use a method of this library in the main class of a C++ project. This is the makefile i use:
abundancebin: main.o profileManager.o myHash.o myMalloc.o myStack.o myStringHash.o
g++ -o abundancebin main.o profileManager.o myHash.o myMalloc.o myStack.o myStringHash.o
main.o: main.cpp
g++ -c main.cpp
profileManager.o: profileManager.cpp
g++ -c profileManager.cpp
myHash.o: myHash.cpp
g++ -c myHash.cpp
myMalloc.o: myMalloc.cpp
g++ -c myMalloc.cpp
myStack.o: myStack.cpp
g++ -c myStack.cpp
myStringHash.o: myStringHash.cpp
g++ -c myStringHash.cpp
clean:
-rm *.o abundancebin
I tried to import the C library in main.cpp using after other imports:
#ifdef __cplusplus
extern "C" {
#endif
#include <cluster.h>
#ifdef __cplusplus
}
#endif
but when i compile with make i have this response:
main.cpp:29:21: fatal error: cluster.h: No such file or directory
#include <cluster.h>
^
compilation terminated.
make: *** [main.o] Error 1
if i use "cluster.h" instead of i have this error:
main.o:main.cpp:(.text+0xf68): riferimento non definito a "kmedoids"
main.o:main.cpp:(.text+0xf68): rilocazione adattata per troncamento: R_X86_64_PC32 contro il simbolo non definito "kmedoids"
/usr/bin/ld: main.o: bad reloc address 0x18 in section.xdata'
collect2: error: ld returned 1 exit status
make: * [abundancebin] Error 1`
I also tried to copy the code part i need from C library to C++ project but the compiler reports many errors like this:
error: invalid conversion from ‘void*’ to ‘int*’ [-fpermissive]
vector = malloc(nnodes*sizeof(int));
The library files are in the same folder of the project files. Can someone help?
Thank you
If you want to add some code which is wrote in c language, you have to #include it like:
extern "C" {
#include "x264.h"
}
which tell compiler to deal with it differently, and its not necessary to change your code to c++
1: you add your code as: #include <cluster.h>
its better to change it to: #include "cluster.h"
the different is, the second one tell the compiler to first search for your header in the current directory and then in the main c++ libraries directory
your error:
main.cpp:29:21: fatal error: cluster.h: No such file or directory
is because it can't find the header, so, if cluster.h is in the same directory as main.cpp, use #include "cluster.h"
or you can use -I. (which tell its in the current directory) or -I/address to tell compiler where to look for your header
when you correct it, you get the second error, which I believe its because of your code, and I think its because of your code in main.cpp, and do not have anything with your cluster code, I suggest, try to post your code to find out what's the problem
In answer to your question, I would approach it something like this in the makefile:
all: cfile.o
g++ main.cpp -o app
cfile.o: cluster.c cluster.h
gcc -c cluster.c
Note: This assumes that everything is in the same directory, so take care to specify paths if not.
Let me know of any further errors.