问题描述
我正在尝试创建一个非常基本的手工Makefile,以创建一个共享库来说明这一点.
I am trying to create a very basic hand crafted Makefile to create a shared library to illustrate a point.
这是我到目前为止所拥有的:
This is what I have so far:
SHELL = /bin/sh
CC = gcc
FLAGS = -std=gnu99 -Iinclude
CFLAGS = -fPIC -pedantic -Wall -Wextra -march=native -ggdb3
DEBUGFLAGS = -O0 -D _DEBUG
RELEASEFLAGS = -O2 -D NDEBUG -combine -fwhole-program
TARGET = example.so
SOURCES = $(shell echo src/*.c)
HEADERS = $(shell echo include/*.h)
OBJECTS = $(SOURCES:.c=.o)
PREFIX = $(DESTDIR)/usr/local
BINDIR = $(PREFIX)/bin
all: $(TARGET)
$(TARGET): $(OBJECTS)
$(CC) $(FLAGS) $(CFLAGS) $(DEBUGFLAGS) -o $(TARGET) $(OBJECTS)
当我运行make
时,它尝试构建应用程序-ld
失败,因为它无法解析main()
.
When I run make
, it attempts to build an application - and ld
fails because it can't resolve main()
.
问题似乎出在CFLAGS
上-我指定了-fPIC
,但是那不起作用-我在做什么错了?
Problem seems to be with CFLAGS
- I have specified -fPIC
but that is not working - what am I doing wrong?
我按照建议添加了-shared
标志,当我运行make
时,出现此错误:
I added the -shared
flag as suggested, when I run make
, I got this error:
gcc -std=gnu99 -Iinclude -fPIC -shared -pedantic -Wall -Wextra -march=native -ggdb3 -O0 -D _DEBUG -o example.so src/example.o
/usr/bin/ld: src/example.o: relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
src/example.o: could not read symbols: Bad value
collect2: ld returned 1 exit status
make: *** [example.so] Error 1
似乎建议仅还原为-fPIC
.
顺便说一句,我新的CFLAGS
设置是:
BTW, my new CFLAGS
setting is:
CFLAGS = -fPIC -shared -pedantic -Wall -Wextra -march=native -ggdb3
我正在Ubuntu 10.0.4上运行gcc v4.4.3.
I am running gcc v4.4.3 on Ubuntu 10.0.4.
推荐答案
解决方案是按如下方式修改XXFLAGS:
The solution was to modify the XXFLAGS as follows:
FLAGS = # -std=gnu99 -Iinclude
CFLAGS = -fPIC -g #-pedantic -Wall -Wextra -ggdb3
LDFLAGS = -shared
这篇关于创建一个简单的Makefile来构建一个共享库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!