使用g ++编译Makefile时遇到问题

时间:2015-01-04 18:55:05

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

我有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可以在我的机器上运行,还是与我的命令有关?

1 个答案:

答案 0 :(得分:5)

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

您混淆的根本原因是您认为应该编译makefile ,但makefile是一组用于编译源文件的规则。一旦掌握了一些东西,命令g++ Makefile对你来说就会让人感到恐惧,因为你知道make 会调用 g++