【问题标题】:Trouble compiling a Makefile using g++使用 g++ 编译 Makefile 时遇到问题
【发布时间】:2015-01-04 18:55:05
【问题描述】:

我有一个 Makefile 的文本,我正在尝试编译它并生成一个 .e 可执行文件。我正在尝试用 g++ 编译它,但我遇到了一些困难。请注意,我有一台运行 Mavericks 的 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.

我做错了什么? Makefile 不能在我的机器上运行,还是和我的命令有关?

【问题讨论】:

    标签: c++ macos makefile g++ clang


    【解决方案1】:

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

    您的基本困惑在于您的印象是您应该编译一个makefile,但makefile 是一组编译源文件的规则。一旦你掌握了窍门,g++ Makefile 命令会让你觉得很可怕,因为你会知道make 调用 g++

    【讨论】:

    • 我运行了 mv Makefile.cpp Makefile,没有出现错误。但是,然后我尝试运行 make -f Makefile,我收到以下错误:Makefile:1: *** missing separator。停止。
    • 好的,那么您的 makefile 中有一个错误。编辑问题,完整发布您的 makefile,然后发布make --just-print 的结果。
    • cat -A Makefile 会告诉你是否有制表符/空格问题。
    • 好的--当我运行它时,我得到 cat: 非法选项 -- A 用法:cat [-benstuv] [file ...]。我无法发布原始的 makefile,因为显式代码不是我制作的,而且是私有的。
    • 好的,然后检查一下:stackoverflow.com/questions/18936337/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-10
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 2016-05-04
    • 1970-01-01
    相关资源
    最近更新 更多