我有一个使用Portals网络编程接口(interface)版本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;
}

Makefile:
.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)的Portals 4.0规范(请参阅here)中进行了定义。

我不确定为什么我会收到 undefined reference ,并且希望对我的情况有所投入。如果我可以提供更多信息来解决问题,请告诉我。

谢谢。

编辑:

来自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

最佳答案

您正在将自己的应用程序构建为C++应用程序,可能需要用外部符号“C”包装#include,如下所示:

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

通常,图书馆作者自己管理这个问题,但是在这种情况下门户网站可能没有?

关于c++ - 使用静态库的未定义引用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17347055/

10-11 16:08