我试图从Debian6编译一个.cpp程序。我有一个可以工作的makefile,它只适用于.c档案,但我需要在.cpp上编译我的程序。所以这里是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}所以,当我在终端上写make myprogramname.cpp时,我得到的是:
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我也尝试了一下-o Programa_Hand2 Programa_Hand2.cpp,得到了同样的错误,我想我必须用makefile来改变一些东西,但是我不知道有什么问题,有什么想法吗?我是编程和linux的新手,所以我对此感到有点生气,因为我需要在c++上编译这个程序,我在c上编译了它并正常工作。谢谢你的帮助!
编辑:从互联网上做了一些研究,似乎RTAI不支持C++;所以有没有办法用C++编译它?另外,我的#includes是:
#include <stdio.h>
#include <robot/robot.h>
#include <unistd.h>
#include <error.h>
#include <iostream>
#include "RobotConfig.h"再次感谢!
发布于 2016-07-21 08:47:51
简而言之,对于C++文件,您需要使用g++命令,而不是使用gcc。
您需要使用C++编译器而不是C编译器来编译C++文件,这两者是有区别的。
GNU的命令是g++,假设您已经安装了它。这是很有可能的,因为您似乎有一个C编译器。
将一个名为CXX的变量添加到Makefile中,并将其设置为等于g++。然后使用CXX变量作为命令编译C++代码,而不是使用CC变量。
$(CXX) -o <.output文件name> <.cpp文件go here>
https://stackoverflow.com/questions/38461831
复制相似问题