使用静态库的未定义引用

时间:2013-06-27 15:19:10

标签: c++ static-libraries

我有一个非常简单的程序,使用门户网络编程接口4.0版中提供的功能(参见here获取源代码)。以下是我的代码

main.cxx:

#include <stdio.h>
#include <portals4.h>

int main() {
  if (PtlInit() == PTL_OK) {
    printf("Successfully initialized...\n");
    PtlFini();
  }
  else {
    printf("Failed to initialize...\n");
    abort();
  }

  return 0;
}

生成文件:

.SUFFIXES: .cxx .o .h

CXX = g++

TARGET = test
SRCS   = main.cxx
OBJS   = $(SRCS:.cxx=.o)

PORTALS_ROOT = /home/brooks8/portals4
PORTALS_INCS = -I$(PORTALS_ROOT)/include
PORTALS_LIBS = -L$(PORTALS_ROOT)/lib -lportals -lportals_runtime

CXXFLAGS = -std=c++0x $(PORTALS_INCS)
LDFLAGS  = $(PORTALS_LIBS)

%.o: %.cxx
    $(CXX) -c $(CXXFLAGS) $<

$(TARGET) : $(OBJS)
    $(CXX) -o $@ $^ $(LDFLAGS)

$(SRCS) : $(HEADERS)

clean :
    rm -rf *.o $(TARGET)

当我运行make时:

g++ -c -std=c++0x -I/home/brooks8/portals4/include main.cxx
g++ -o test main.o -L/home/brooks8/portals4/lib -lportals -lportals_runtime
main.o: In function `main':
main.cxx:(.text+0x5): undefined reference to `PtlInit()'
main.cxx:(.text+0x1d): undefined reference to `PtlFini()'
collect2: ld returned 1 exit status
make: *** [test] Error 1

我的主目录(/home/brooks8/portals4)中安装了Portals 4.0,portals4.h头文件位于/home/brooks8/portals4/include/portals4.h。在头文件中,两个函数都已明确定义,并分别在第38页(Section 3.5.1)和第39页(Secion 3.5.2)的门户4.0规范(请参阅here)中定义。

我不确定为什么我收到未定义的引用,并希望对我的情况有任何意见。如果我能提供更多信息来帮助解决我的问题,请告诉我。

谢谢。

编辑:

nm /home/brooks8/portals4/lib/libportals.a的结果:

...
libportals_ib_la-ptl_fat_lib.o:
0000000000000000 r .LC0
0000000000000000 r .LC1
000000000000000e r .LC2
0000000000000011 r .LC3
0000000000000070 T PtlFini
0000000000000080 T PtlInit
...

file main.o的结果:

main.o: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped

file ~/portals4/lib/libportals.a的结果:

~/portals4/lib/libportals.a: current ar archive

~/portals/lib/libportals.a中的对象文件:

libportals_ib_la-ptl_atomic.o
libportals_ib_la-ptl_buf.o
libportals_ib_la-ptl_conn.o
libportals_ib_la-ptl_ct.o
libportals_ib_la-ptl_ct_common.o
libportals_ib_la-ptl_data.o
libportals_ib_la-ptl_eq.o
libportals_ib_la-ptl_eq_common.o
libportals_ib_la-ptl_evloop.o
libportals_ib_la-ptl_fat_lib.o
libportals_ib_la-ptl_id.o
libportals_ib_la-ptl_iface.o
libportals_ib_la-ptl_init.o
libportals_ib_la-ptl_iov.o
libportals_ib_la-ptl_le.o
libportals_ib_la-ptl_md.o
libportals_ib_la-ptl_me.o
libportals_ib_la-ptl_misc.o
libportals_ib_la-ptl_move.o
libportals_ib_la-ptl_mr.o
libportals_ib_la-ptl_ni.o
libportals_ib_la-ptl_obj.o
libportals_ib_la-ptl_param.o
libportals_ib_la-ptl_pt.o
libportals_ib_la-ptl_recv.o
libportals_ib_la-ptl_tgt.o
libportals_ib_la-ptl_rdma.o
libportals_ib_la-ptl_iface_ib.o
libportals_ib_la-ptl_mem.o
libportals_ib_la-ptl_queue.o
libportals_ib_la-ptl_shmem.o

file XXXX.o的结果:

XXXX.o: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped

1 个答案:

答案 0 :(得分:2)

您正在将应用程序构建为C ++应用程序,您可能需要使用extern“C”包装#include,如下所示:

extern "C" {
    #include <portal4.h>
    // other c-style headers here
}

通常,图书馆作者自己管理这个,但在这种情况下,门户网站可能不会这样做吗?