编译so失败

OneOnce 2018-02-23 11:00:52
写了一个MyMath的C++代码,实现了Sum方法, 无法生成.so,可以看到.o已生成了,平台Ubuntu 16.04。
MyMath.h
#pragma once
#ifndef __MYMATH_H__
#define __MYMATH_H__



class MyMath
{
public:
MyMath();
virtual ~MyMath();
public:
int Sum(int a, int b);
};

#endif // ! __MYMATH_H__


MyMath.cpp
#include "MyMath.h"
MyMath::MyMath()
{
}
MyMath::~MyMath()
{
}
int MyMath::Sum(int a, int b)
{
return a + b;
}

把MyMath编译成.so
Makefile
TARGET := libMyMath.so

CC := g++
LIBS :=
LDFLAGS :=
DEFINES :=
INCLUDE := -I.
CFLAGS := -g -Wall -O3 -std=c++11 $(DEFINES) $(INCLUDE)
CXXFLAGS:= $(CFLAGS)

SHARE := -fPIC -shared -o

SOURCE := $(wildcard *.c) $(wildcard *.cpp)

SRC_NODIR:= $(notdir $(SOURCE))

OBJS := $(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(SRC_NODIR)))

.PHONY := everything objs clean veryclean rebuild

everything : $(TARGET)

all : $(TARGET)

objs : $(OBJS)

rebuild : veryclean everything

clean:
rm -fr *.o

veryclean : clean
rm -fr $(TARGET)

$(TARGET) : $(OBJS)
$(CC) $(CXXFLAGS) $(SHARE) $@ $(OBJS) $(LDFLAGS) $(LIBS)



报错信息
/usr/bin/ld: MyMath.o: relocation R_X86_64_32S against `_ZTV6MyMath' can not be used when making a shared object; recompile with -fPIC
MyMath.o: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
Makefile:52: recipe for target 'libMyMath.so' failed
...全文
431 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
OneOnce 2018-02-23
  • 打赏
  • 举报
回复
引用 4 楼 zhxianbin 的回复:
不是一样的错误么,编译的时候要加上 -fPIC
加上了-fPIC啊 SHARE := -fPIC -shared -o R_X86_64_32S和av_destruct_packet冲突不知道怎么搞
zhxianbin 2018-02-23
  • 打赏
  • 举报
回复
不是一样的错误么,编译的时候要加上 -fPIC
OneOnce 2018-02-23
  • 打赏
  • 举报
回复
这里有讲到:http://www.aiuxian.com/article/p-349274.html /usr/bin/ld: /usr/local/lib/libavcodec.a(avpacket.o): relocation R_X86_64_32S against `av_destruct_packet' can not be used when making a shared object; recompile with -fPIC 这个错误是指安装的库libavcodec.a和需要用的库有不同的地方,需要你把libavcodec重装。 当然也可能系统装了几个版本的libavcodec,现在编译的东西找错了一个。 1.看看是否多装了,多装了把不要的libavcodec都干掉 2.如果没多装,重装libavcodec 怎么确认libavcodec又没有安装,如何安装在Ubuntu 16.04下面?
OneOnce 2018-02-23
  • 打赏
  • 举报
回复
引用 1 楼 zhxianbin 的回复:
看提示 : recompile with -fPIC
这个是说重新编译,但看上去还是失败了。不知道哪里错了。
zhxianbin 2018-02-23
  • 打赏
  • 举报
回复
看提示 : recompile with -fPIC
OneOnce 2018-02-23
  • 打赏
  • 举报
回复
Makefile改成这样就可以
TARGET := libMyMath.so
CC      := g++
LIBS    := 
LDFLAGS := 
DEFINES := 
INCLUDE := -I.
CFLAGS  := -g -Wall -O3 -std=c++11 $(DEFINES) $(INCLUDE)
CXXFLAGS:= $(CFLAGS)

#SHARE   := -shared -fpic -o

SOURCE   := $(wildcard *.c) $(wildcard *.cpp)

SRC_WITHOUTDIR:= $(notdir $(SOURCE))

OBJS     := $(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(SRC_WITHOUTDIR)))

.PHONY  := everything objs clean veryclean rebuild

everything : $(TARGET)

all : $(TARGET)

objs : $(OBJS)

rebuild : veryclean everything

clean:
	rm -fr *.o

veryclean : clean
	rm -fr $(TARGET)

#$(TARGET) : $(OBJS)
#	$(CC) $(CXXFLAGS) $(SHARE) $@ $(OBJS) $(LDFLAGS) $(LIBS)


$(TARGET) : $(OBJS)
	$(CC) -shared -o $@ $(OBJS)

%.o : %.cpp
	$(CC) $(CXXFLAGS) -fpic -c $< -o $@ $(CXXFLAGS) $(LDFLAGS) $(LIBS)

23,116

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧