使用 g++ 编译生成文件时出现问题

Trouble compiling a Makefile using g++

本文关键字:问题 文件 g++ 编译 使用      更新时间:2023-10-16

我有一个Makefile的文本,我正在尝试编译它并生成一个.e可执行文件。我正在尝试用 g++ 编译它,但我遇到了一些困难。请注意,我有一台运行小牛队的MacBook Pro。

我尝试了两种方法——第一种是打开 Xcode 并创建一个新的.cpp文件。然后,我将Makefile文本复制到空的.cpp文件中,并将其保存为Makefile.g++。在 Mac 终端中,我随后执行

g++ Makefile.g++

它返回错误

d: warning: ignoring file Makefile.g++, file was built for unsupported file format (      
0x23 0x20 0x47 0x65 0x6E 0x65 0x72 0x69 0x63 0x20 0x70 0x69 0x6D 0x63 0x20 0x67 ) which  
is not the architecture being linked (x86_64): Makefile.g++
Undefined symbols for architecture x86_64:
"_main", referenced from:
 implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

然后我尝试将其另存为 Makefile.cpp,然后执行

g++ Makefile.cpp

然后我收到错误

Makefile.cpp:11:3: error: invalid preprocessing directive
# Generic pimc g++ makefile
  ^
Makefile.cpp:13:1: error: C++ requires a type specifier for all declarations
CC      = g++
^~
Makefile.cpp:13:11: error: use of undeclared identifier 'g'
CC      = g++
          ^
Makefile.cpp:16:2: error: invalid preprocessing directive
#CC = /opt/local/bin/g++-mp-4.7
 ^
Makefile.cpp:17:2: error: invalid preprocessing directive
#LD = /opt/local/bin/g++-mp-4.7
 ^
Makefile.cpp:33:3: error: invalid preprocessing directive
# Edit below to include details on your specific host
  ^
Makefile.cpp:116:3: error: invalid preprocessing directive
# -------------------------------------------------------------------------------
  ^
Makefile.cpp:119:3: error: invalid preprocessing directive
# Link
  ^
Makefile.cpp:123:3: error: invalid preprocessing directive
# Compile
  ^
Makefile.cpp:127:3: error: invalid preprocessing directive
# -------------------------------------------------------------------------------
  ^
Makefile.cpp:130:22: error: expected ';' after top level declarator
$(RM) $(PROG) $(OBJS)
                     ^
                     ;
11 errors generated.

我做错了什么?生成文件不能在我的机器上运行,还是与我的命令有关?

哇,牛仔!你告诉g++ Makefile.cpp是一个C++源文件,而不是生成文件。 mv Makefile.cpp Makefile,然后运行make。这应该会有所帮助。

混淆的根本原因是你认为你应该编译一个生成文件,但生成文件是一组编译源文件的规则。一旦你掌握了窍门,命令g++ Makefile对你来说会很可怕,因为你会知道make调用g++