xcode g++ : 无法与主可执行文件链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23132047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
g++ : can't link with a main executable file
提问by Souf
I'm developing an application using statistical attacks to crack wep keys.
我正在开发一个使用统计攻击来破解 wep 密钥的应用程序。
When I compile with my makefile (above) I get this error :
当我用我的 makefile(上面)编译时,我收到这个错误:
ld: can't link with a main executable file 'execStatAttack' for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation) make: *[statAttack] Error 1
ld:无法与架构 x86_64 的主可执行文件“execStatAttack”链接
clang:错误:链接器命令失败,退出代码 1(使用 -v 查看调用) make:*[statAttack] 错误 1
My project contain those files :
我的项目包含这些文件:
statAttack.cpp : contain the main function, uses files above
rc4.h + rc4.cpp : with those function
statAttack.cpp : 包含主要功能,使用上面的文件
rc4.h + rc4.cpp :具有这些功能
#include <iostream> #include <stdlib.h> #include <stdio.h #include <vector #ifndef RC4 #define RC4 using namespace std int* rc4(int); int random_byte(); vector<int> cipher_mess_seq (long, int); #endif
#include <iostream> #include <stdlib.h> #include <stdio.h #include <vector #ifndef RC4 #define RC4 using namespace std int* rc4(int); int random_byte(); vector<int> cipher_mess_seq (long, int); #endif
- bias.h + bias.cpp :
- 偏差.h + 偏差.cpp :
#include <iostream> #include <stdlib.h> #include <stdio.h> #include <utility> #include <fstream> #include <vector> #include <string> #ifndef BIAIS #define BIAIS using namespace std; typedef pair<int,double> IntegerDoublePair; vector<IntegerDoublePair> get_bias (string, int); int compareTo (double, double); vector<IntegerDoublePair> get_all_biases(string); #endif
#include <iostream> #include <stdlib.h> #include <stdio.h> #include <utility> #include <fstream> #include <vector> #include <string> #ifndef BIAIS #define BIAIS using namespace std; typedef pair<int,double> IntegerDoublePair; vector<IntegerDoublePair> get_bias (string, int); int compareTo (double, double); vector<IntegerDoublePair> get_all_biases(string); #endif
- and the makefile :
- 和生成文件:
CC = g++ CFLAGS = -Wall -g LDFLAGS = -lm EXEC_NAME_NAIVE = execNaiveAttack EXEC_NAME_STATALGO = execStatAttack OBJ_FILES_NAIVE = naiveAttack.o biais.o rc4.o OBJ_FILES_STATALGO = statAttack.o biais.o rc4.o naiveAttack : $(EXEC_NAME_NAIVE) statAttack : $(EXEC_NAME_STATALGO) $(EXEC_NAME_NAIVE) : $(OBJ_FILES_NAIVE) $(CC) $(OBJ_FILES_NAIVE) $(LDFLAGS) -o $(EXEC_NAME_NAIVE) $(EXEC_NAME_STATALGO) : $(OBJ_FILES_STATALGO) $(CC) $(OBJ_FILES_STATALGO) $(LDFLAGS) -o $(EXEC_NAME_STATALGO) %.o : %.cpp $(CC) $(CFLAGS) -o $@ -c $< clean : rm -f $(OBJ_FILES_NAIVE) $(OBJ_FILES_STATALGO) mrproper: clean rm -rf $(EXEC_NAME_NAIVE) $(EXEC_NAME_STATALGO)
CC = g++ CFLAGS = -Wall -g LDFLAGS = -lm EXEC_NAME_NAIVE = execNaiveAttack EXEC_NAME_STATALGO = execStatAttack OBJ_FILES_NAIVE = naiveAttack.o biais.o rc4.o OBJ_FILES_STATALGO = statAttack.o biais.o rc4.o naiveAttack : $(EXEC_NAME_NAIVE) statAttack : $(EXEC_NAME_STATALGO) $(EXEC_NAME_NAIVE) : $(OBJ_FILES_NAIVE) $(CC) $(OBJ_FILES_NAIVE) $(LDFLAGS) -o $(EXEC_NAME_NAIVE) $(EXEC_NAME_STATALGO) : $(OBJ_FILES_STATALGO) $(CC) $(OBJ_FILES_STATALGO) $(LDFLAGS) -o $(EXEC_NAME_STATALGO) %.o : %.cpp $(CC) $(CFLAGS) -o $@ -c $< clean : rm -f $(OBJ_FILES_NAIVE) $(OBJ_FILES_STATALGO) mrproper: clean rm -rf $(EXEC_NAME_NAIVE) $(EXEC_NAME_STATALGO)
this is my configuration (terminal) :
这是我的配置(终端):
==> g++ --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.1.0
Thread model: posix
==> g++ --version
配置为:--prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM 5.1 版 (clang-503.0.40)(基于 LLVM 3.4svn)
目标:x86_64-apple-darwin13.1.0
线程模型:posix
So i would like your help, to find out why this error appeared.
所以我想得到你的帮助,找出这个错误出现的原因。
Thanks.
谢谢。
回答by Tim Sylvester
This error usually means you're missing a -c
when compiling a simple program, something like this:
这个错误通常意味着你-c
在编译一个简单的程序时遗漏了一个,像这样:
%.o: %.c
$(CC) $(CFLAGS) -o $@ $^
Program: main.o
$(CC) $(LDFLAGS) -o $@ $^
What's happening is that the first rule is building main.o
just like you asked, but instead of being an object file, it's actually the complete, compiled and linked program.
发生的事情是第一条规则main.o
就像你问的那样构建,但它不是一个目标文件,它实际上是完整的、编译的和链接的程序。
When the second rule tries to use it as an object file, the linker finds that it's not an object file at all and produces "can't link with a main executable file."
当第二条规则试图将它用作目标文件时,链接器发现它根本不是目标文件,并产生“无法与主可执行文件链接”。
Obviously for a more complex program, one with multiple object files or with library dependencies, it would not be able to build an executable from just the one source file, so you'll get a different error and never get as far as the link rule.
显然,对于一个更复杂的程序,一个有多个目标文件或依赖库的程序,它不能只从一个源文件构建一个可执行文件,所以你会得到一个不同的错误,永远不会达到链接规则.
The solution, of course, is to add -c
to the first rule so that the first invocation only compiles and does not link, producing an actual object file.
当然,解决方案是添加-c
到第一条规则,以便第一次调用只编译而不链接,生成实际的目标文件。
%.o: %.c
$(CC) $(CFLAGS) -c -o $@ $^
回答by u4370109
(Answered in the comments. See Question with no answers, but issue solved in the comments (or extended in chat))
(在评论中回答。请参阅没有答案的问题,但问题已在评论中解决(或在聊天中扩展))
@Paul R Wrote:
@Paul R 写道:
You have several typos in your source files, e.g.
#include <vector
andusing namespace std
- you will need to fix these and any other similar mistakes to have a chance of compiling the code.
您的源文件中有几个拼写错误,例如
#include <vector
和using namespace std
- 您需要修复这些错误和任何其他类似错误,才有机会编译代码。
@Etan Reisner wrote:
@Etan Reisner 写道:
You should also probably stick to spelling
bias
bias
instead of also usingbiais
in some places.
您也应该坚持拼写
bias
bias
而不是biais
在某些地方使用。
The OP wrote:
OP写道:
I finally found the error, it was in my makefile, those lines:
naiveAttack : $(EXEC_NAME_NAIVE) statAttack : $(EXEC_NAME_STATALGO)
because the executable was already generated, and another compilation was done after, so it generates this error, for more informations I can provide the new makefile, for those who have this type of error.
我终于找到了错误,它在我的 makefile 中,这些行:
naiveAttack : $(EXEC_NAME_NAIVE) statAttack : $(EXEC_NAME_STATALGO)
因为可执行文件已经生成,之后又进行了另一次编译,所以它生成了这个错误,有关更多信息,我可以提供新的 makefile,对于那些有这个的人错误类型。