简体   繁体   English

Makefile链接问题

[英]Makefile linking issues

I wrote the Makefile for an easier way to run the unit tests (the default target is test). 我编写了Makefile,以便以更简单的方式运行单元测试(默认目标是test)。 The directories structure is as follows: 目录结构如下:

lib - google c++ test framework lib -Google C ++测试框架

src - source files src源文件

test - unit tests test -单元测试

In src I have the files ah , a.cc , b.cc (which holds main) and I want to test class A() which is defined in ah . src我有文件aha.ccb.cc (保存main),我想测试在ah定义的A()类。 All in all, because the order of linking is: 总而言之,因为链接的顺序是:

g++ -Wall -c -o obj/src/ao src/a.cc

g++ -Wall -c -o obj/src/bo src/b.cc

g++ -o all obj/src/ao

I get the error: 我得到错误:

(.text+0x18): undefined reference to main' collect2: ld returned 1 exit status make: *** [all] Error

even though I don't actually need a main() defined in src since I already have a main() defined in test which runs the unit tests. 即使我实际上不需要src定义的main() ,因为我已经在test中定义了一个main()来运行单元测试。

I don't understand very well what you want to do, but if your main is in, say, test.o, you need to specify that in you linking line. 我不太了解您要做什么,但是如果您的主要内容在test.o之内,则需要在链接行中指定。 For example: 例如:

g++ -o all obj/src/a.o test.o

When you say 当你说

g++ -o all obj/src/a.o

g++ thinks you've asked it to build a executable, and accordingly is looking for the entry point. g++认为您已要求它构建可执行文件,因此正在寻找入口点。 Presumably you meant to ask it for something else (a library, maybe?). 大概是您要问的其他问题(也许是一个库?)。

Even though it doesn't solve your current problem, it might be helpful to know that you are re-engineering automake . 即使它不能解决您当前的问题,了解您正在重新设计automake可能也会有所帮助。 You shouldn't be writing Makefiles for C or C++ projects by hand nowadays. 现在,您不应该手工为C或C ++项目编写Makefile。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM