多说无益,直接上代码
-----------------------------------------------------------------------------------
myso.cpp
C/C++ code#include <map>
#include <string>
#include <iostream>
#include "myso.h"
void print_mapdata(const std::map<std::string, std::string> &_mapData)
{
std::map<std::string, std::string>::const_iterator itm = _mapData.begin();
std::cout << '\n' << "--------------------------" << std::endl;
for (; itm!=_mapData.end(); ++itm)
{
std::cout << itm->first << ": " << itm->second << std::endl;
}
std::cout << "--------------------------" << std::endl;
}
-----------------------------------------------------------------------------------
myso.h
C/C++ code#ifndef __MYSO_H__
#define __MYSO_H__
void print_mapdata(const std::map<std::string, std::string> &_mapData);
#endif
-----------------------------------------------------------------------------------
test.cpp
C/C++ code#include <map>
#include <string>
#include "myso.h"
int main()
{
std::map<std::string, std::string> mapData;
mapData["111111"]="AAAAAAAAAAAAAAAA";
mapData["222222"]="BBBBBBBBBBBBBBBB";
mapData["333333"]="CCCCCCCCCCCCCCCC";
mapData["444444"]="DDDDDDDDDDDDDDDD";
mapData["555555"]="EEEEEEEEEEEEEEEE";
print_mapdata(mapData);
return 0;
}
-----------------------------------------------------------------------------------
makefile
Perl codeCC = cc
CXX = xlC
INCDIR = -I.
CFLAGS = -q64 $(INCDIR)
LIBDIR= .
EXCUTEFILE = ts
LIBFILE = libmyso.so
LINK= -L$(LIBDIR) -lmyso
OBJS = test.o
ts : $(OBJS)
$(CXX) -q64 -brtl -V -bnoquiet -o $(EXCUTEFILE) $> $(LINK)
so : myso.o
$(CXX) -q64 -G -o $(LIBFILE) $>
test.o:test.cpp
$(CXX) $(CFLAGS) -o $@ -c $>
myso.o:myso.cpp
$(CXX) $(CFLAGS) -o $@ -c $>
clean:
rm -f $(LIBFILE)
rm -f $(EXCUTEFILE)
rm -f *.o
$ make so
xlC -q64 -I. -o myso.o -c myso.cpp
xlC -q64 -G -o libmyso.so myso.o
我的环境没有xlC,使用g++编译运行没有问题,也没有警告
只是奇怪的是,对于.so库文件,g++老是找不到
把libmyso.so改名为libmyso.a就可以了