What's the difference between gcc and g++/gcc-c++?

gcc will compile C source files as C and C++ source files as C++ if the file has an appropriate extension; however it will not link in the C++ library automatically.

g++ will automatically include the C++ library; by default it will also compile files with extensions that indicate they are C source as C++, instead of as C.

From http://gcc.gnu.org/onlinedocs/gcc/Invoking-G_002b_002b.html#Invoking-G_002b_002b:

C++ source files conventionally use one of the suffixes .C',.cc', .cpp',.CPP', .c++',.cp', or.cxx'; C++ header files often use.hh', .hpp',.H', or (for shared template code) .tcc'; and preprocessed C++ files use the suffix.ii'. GCC recognizes files with these names and compiles them as C++ programs even if you call the compiler the same way as for compiling C programs (usually with the name gcc).

However, the use of gcc does not add the C++ library. g++ is a program that calls GCC and treats.c',.h' and .i' files as C++ source files instead of C source files unless -x is used, and automatically specifies linking against the C++ library. This program is also useful when precompiling a C header file with a.h' extension for use in C++ compilations.

For example, to compile a simple C++ program that writes to the std::cout stream, I can use either (MinGW on Windows):

  • g++ -o test.exe test.cpp
  • gcc -o test.exe test.cpp -lstdc++

But if I try:

  • gcc -o test.exe test.cpp

I get undefined references at link time.

And for the other difference, the following C program:

#include <stdlib.h>
#include <stdio.h>

int main() 
{
    int* new;
    int* p = malloc(sizeof(int));

    *p = 42;
    new = p;

    printf("The answer: %d\n", *new);

    return 0;
}

compiles and runs fine using:

  • gcc -o test.exe test.c

But gives several errors when compiled using:

  • g++ -o test.exe test.c

Errors:

test.c: In function 'int main()':
test.c:6:10: error: expected unqualified-id before 'new'
test.c:6:10: error: expected initializer before 'new'
test.c:7:32: error: invalid conversion from 'void*' to 'int*'
test.c:10:9: error: expected type-specifier before '=' token
test.c:10:11: error: lvalue required as left operand of assignment
test.c:12:36: error: expected type-specifier before ')' token

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据您提供的Makefile,这个错误信息是由于在删除目标文件时出现问题引起的。这可能是由于您的Dev-C++环境配置不正确导致的。 在您的Makefile中,您使用了`$(DEL)`变量来执行删除操作。然而,在Windows环境下,删除操作通常使用`del`命令而不是`devcpp.exe INTERNAL_DEL`。 为了解决这个问题,您可以尝试将以下行: ``` DEL = C:\Program Files (x86)\Embarcadero\Dev-Cpp\devcpp.exe INTERNAL_DEL ``` 替换为: ``` DEL = del ``` 这将使用Windows的`del`命令来执行删除操作。 修改后的Makefile如下所示: ```makefile # Project: 项目1 # Makefile created by Embarcadero Dev-C++ 6.3 CPP = g++.exe CC = gcc.exe WINDRES = windres.exe OBJ = main.o LINKOBJ = main.o LIBS = -L"C:/Program Files (x86)/Embarcadero/Dev-Cpp/TDM-GCC-64/lib" -L"C:/Program Files (x86)/Embarcadero/Dev-Cpp/TDM-GCC-64/x86_64-w64-mingw32/lib" -static-libgcc INCS = -I"C:/Program Files (x86)/Embarcadero/Dev-Cpp/TDM-GCC-64/include" -I"C:/Program Files (x86)/Embarcadero/Dev-Cpp/TDM-GCC-64/x86_64-w64-mingw32/include" -I"C:/Program Files (x86)/Embarcadero/Dev-Cpp/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/9.2.0/include" CXXINCS = -I"C:/Program Files (x86)/Embarcadero/Dev-Cpp/TDM-GCC-64/include" -I"C:/Program Files (x86)/Embarcadero/Dev-Cpp/TDM-GCC-64/x86_64-w64-mingw32/include" -I"C:/Program Files (x86)/Embarcadero/Dev-Cpp/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/9.2.0/include" -I"C:/Program Files (x86)/Embarcadero/Dev-Cpp/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/9.2.0/include/c++" BIN = 项目1.exe CXXFLAGS = $(CXXINCS) -std=c++11 CFLAGS = $(INCS) -std=c++11 DEL = del .PHONY: all all-before all-after clean clean-custom all: all-before $(BIN) all-after clean: clean-custom ${DEL} $(OBJ) $(BIN) $(BIN): $(OBJ) $(CPP) $(LINKOBJ) -o $(BIN) $(LIBS) main.o: main.cpp $(CPP) -c main.cpp -o main.o $(CXXFLAGS) ``` 请尝试使用修改后的Makefile重新编译您的项目,看看是否能够解决问题。如果还有其他错误信息,请提供详细的错误信息,以便我更好地帮助您解决问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值