【问题标题】:PETSc undefined referencePETSc 未定义参考
【发布时间】:2014-12-16 14:48:24
【问题描述】:

我有一个非常初学者的问题,但我现在真的迷路了。我从 PETSc 开始,但我的代码编译有问题。我正在尝试使用自己的 Makefile,但编译器一直在大喊“未定义的引用”错误。我试图自己弄清楚几个小时,但我根本没有看到错误。因此,如果您能认识到错误,我们将非常感谢您的帮助。

这是整个错误信息:

mpicc petscLUFact.o -L/home/martin/petsc-3.5.2/arch-linux2-c-debug/lib
petscLUFact.o: In function `main':
/home/martin/Dokumenty/Programovani/petscLUFact.c:18: undefined reference to `PetscInitialize'
/home/martin/Dokumenty/Programovani/petscLUFact.c:20: undefined reference to `PETSC_COMM_WORLD'
/home/martin/Dokumenty/Programovani/petscLUFact.c:20: undefined reference to `MatCreate'
/home/martin/Dokumenty/Programovani/petscLUFact.c:21: undefined reference to `MatSetSizes'
/home/martin/Dokumenty/Programovani/petscLUFact.c:22: undefined reference to `MatSetFromOptions'
/home/martin/Dokumenty/Programovani/petscLUFact.c:23: undefined reference to `MatMPIAIJSetPreallocation'
/home/martin/Dokumenty/Programovani/petscLUFact.c:24: undefined reference to `MatGetOwnershipRange'
/home/martin/Dokumenty/Programovani/petscLUFact.c:26: undefined reference to `MatDestroy'
/home/martin/Dokumenty/Programovani/petscLUFact.c:28: undefined reference to `PetscFinalize'
/home/martin/Dokumenty/Programovani/petscLUFact.c:20: undefined reference to `PetscError'
/home/martin/Dokumenty/Programovani/petscLUFact.c:24: undefined reference to `PetscError'
/home/martin/Dokumenty/Programovani/petscLUFact.c:23: undefined reference to `PetscError'
/home/martin/Dokumenty/Programovani/petscLUFact.c:22: undefined reference to `PetscError'
/home/martin/Dokumenty/Programovani/petscLUFact.c:21: undefined reference to `PetscError'
collect2: error: ld returned 1 exit status
make: *** [petscLUFact] Error 1

这是我的 .c 文件 - 它还没有完成,只是一个测试:

static char help[] = "Reads a PETSc matrix and vector from a file and reorders it.\n\
     -f0 <input_file> : first file to load (small system)\n\
     -f1 <input_file> : second file to load (larger system)\n\n";

#include <petscsys.h>
#include <petscmat.h>

int main( int argc, char **args ) {
    Mat             A; // matice
    //IS  isrow,iscol; // permutace radku a sloupcu
    PetscInt        r = 5, c = 5; // rozmery matice
    PetscInt        i,j; // souradnice v matici
    PetscInt        Istart, Iend;
    PetscInt        Ii; // pocitadlo
    PetscScalar     v; // 2-rozmerne pole ???
    PetscErrorCode  ierr;

    PetscInitialize( &argc, &args, (char*)0, help );

    ierr = MatCreate( PETSC_COMM_WORLD, &A );CHKERRQ( ierr );
    ierr = MatSetSizes( A, PETSC_DECIDE, PETSC_DECIDE, r*c, r*c );CHKERRQ(ierr);
    ierr = MatSetFromOptions(A);CHKERRQ(ierr);
    ierr = MatMPIAIJSetPreallocation( A, 5, PETSC_NULL, 5, PETSC_NULL );CHKERRQ(ierr);
    ierr = MatGetOwnershipRange(A,&Istart,&Iend);CHKERRQ(ierr);

    MatDestroy(&A);

    PetscFinalize();

    return 0;
}

这是我的 Makefile:

include ${PETSC_DIR}/conf/variables
include ${PETSC_DIR}/conf/rules

CFLAGS=-I${PETSC_DIR}/include -I${PETSC_DIR}/${PETSC_ARCH}/include

petscLUFact: petscLUFact.o
    mpicc petscLUFact.o -L${LD_LIBRARY_PATH}

petscLUFact.o: petscLUFact.c
    mpicc ${CFLAGS} -c petscLUFact.c -o petscLUFact.o

$PETSC_DIR/include${PETSC_DIR}/${PETSC_ARCH}/include 中有petsc 头文件(.h)。

我的系统变量的值是:

$PETSC_DIR=/home/martin/petsc-3.5.2
$PETSC_ARCH=arch-linux2-c-debug
$LD_LIBRARY_PATH=/home/martin/petsc-3.5.2/arch-linux2-c-debug/lib

这是我的 LD_LIBRARY_PATH 文件夹的结构:

arch-linux2-c-debug/lib
├── libpetsc.a
├── libpetsc.so -> libpetsc.so.3.5.2
├── libpetsc.so.3.5 -> libpetsc.so.3.5.2
├── libpetsc.so.3.5.2
├── modules
│   └── 3.5.2-arch-linux2-c-debug
└── pkgconfig
    └── PETSc.pc

【问题讨论】:

  • 未定义的引用错误很可能是由于源代码中的引用。我建议你包括相关的源代码和完整的错误信息。错误消息将准确指出未找到哪个符号。
  • 我已经添加了所需的信息 ;-)
  • 你实际上在哪里将库传递给链接器(或者在这种情况下是mpicc 命令?看起来除了-L&lt;stuff&gt; 标志之外,你还需要一个-lpetsc
  • 是的!非常感谢,现在可以了。顺便说一句,我想赞成你的回应,但我不能? :-o

标签: c makefile mpi petsc


【解决方案1】:

(在 cmets 中的答案和编辑。见Question with no answers, but issue solved in the comments (or extended in chat)

@Wesley Bland 写道:

您实际上在哪里将库传递给链接器(或者在这种情况下是mpicc 命令?看起来除了-L&lt;stuff&gt; 标志之外,您还需要一个-lpetsc

OP 写道:

我在Makefile 中添加了-lpetsc 标志,所以现在看起来像这样:

include ${PETSC_DIR}/conf/variables
include ${PETSC_DIR}/conf/rules

CFLAGS=-I${PETSC_DIR}/include -I${PETSC_DIR}/${PETSC_ARCH}/include

petscLUFact: petscLUFact.o
    mpicc petscLUFact.o -o petscLUFact -L${LD_LIBRARY_PATH} -lpetsc

petscLUFact.o: petscLUFact.c
    mpicc ${CFLAGS} -c petscLUFact.c -o petscLUFact.o

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-14
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-02
    • 2020-06-03
    • 2011-09-11
    相关资源
    最近更新 更多