对 *.h 文件中声明的函数的未定义引用

问题描述 投票:0回答:5

我是一个不熟练的程序员,也是linux新手,在编译时遇到了问题。我在同一文件夹中有两个文件“ex_addinst.c”和“lindo.h”,我输入命令:

g++ -c ex_addinst.c

然后,生成一个目标文件 ex_addinst.o 并带有警告:

ex_addinst.c: In function ‘int main()’:
ex_addinst.c:80: warning: deprecated conversion from string constant to ‘char*’

然后我用

泄露它们
g++ -Wall -o ex_addinst ex_addinst.o

并获取以下信息:

ex_addinst.o: In function `main':
ex_addinst.c:(.text+0x2b): undefined reference to `LSloadLicenseString'
ex_addinst.c:(.text+0x75): undefined reference to `LSgetVersionInfo'
ex_addinst.c:(.text+0xae): undefined reference to `LScreateEnv'
ex_addinst.c:(.text+0x10a): undefined reference to `LSgetErrorMessage'
...
...
ex_addinst.c:(.text+0x1163): undefined reference to `LSdeleteEnv'
collect2: ld returned 1 exit status

我猜头文件“lindo.h”没有编译到.o文件中,但我现在不知道该怎么办。我试过 gcc,但得到同样的错误。我的g++和gcc的版本是4.4.5。我使用的是 Ubuntu 10.10。

所有函数和结构都已在“lindo.h”中声明。

ex_addinst.c部分内容如下:

    #include <stdio.h>
    #include <stdlib.h>
    /* LINDO API header file */
    #include "lindo.h"
        enter code here
int CALLTYPE LSwriteMPIFile(pLSmodel pModel,
                             char     *pszFname);


/* Define a macro to declare variables for
    error checking */
#define APIERRORSETUP  \
   int nErrorCode; \
   char cErrorMessage[LS_MAX_ERROR_MESSAGE_LENGTH] \
/* Define a macro to do our error checking */
#define APIERRORCHECK  \
   if (nErrorCode) \
   { \
      if ( pEnv) \
      { \
         LSgetErrorMessage( pEnv, nErrorCode, \
          cErrorMessage); \
         printf("nErrorCode=%d:  %s\n", nErrorCode, \
          cErrorMessage); \
      } else {\
         printf( "Fatal Error\n"); \
      } \
      exit(1); \
   } \

#define APIVERSION \
{\
    char szVersion[255], szBuild[255];\
    LSgetVersionInfo(szVersion,szBuild);\
    printf("\nLINDO API Version %s built on %s\n",szVersion,szBuild);\
}\
/* main entry point */
int main()
{
   APIERRORSETUP;
   pLSenv pEnv;
   pLSmodel pModel;
   char MY_LICENSE_KEY[1024];

 /*****************************************************************
  * Step 1: Create a model in the environment.
  *****************************************************************/
   nErrorCode = LSloadLicenseString("home/li/work/tools/lindo/lindoapi/license/lndapi60.lic", MY_LICENSE_KEY);
   if ( nErrorCode != LSERR_NO_ERROR)
   {
      printf( "Failed to load license key (error %d)\n",nErrorCode);
      exit( 1);
   }
......
......
......
   APIERRORCHECK;
   {
      int nStatus;
      double objval=0.0, primal[100];
      /* Get the optimization result */
      nErrorCode = LSgetInfo(pModel, LS_DINFO_GOP_OBJ, &objval);
      APIERRORCHECK;
      LSgetMIPPrimalSolution( pModel, primal) ;
      APIERRORCHECK;
      printf("\n\nObjective = %f \n",objval);
      printf("x[0] = %f \n",primal[0]);
      printf("x[1] = %f \n",primal[1]);
      /* Get the linearity of the solved model */
      nErrorCode = LSgetInfo (pModel, LS_IINFO_GOP_STATUS, &nStatus);
      APIERRORCHECK;
      /* Report the status of solution */
      if (nStatus==LS_STATUS_OPTIMAL || nStatus==LS_STATUS_BASIC_OPTIMAL)
      printf("\nSolution Status: Globally Optimal\n");
      else if (nStatus==LS_STATUS_LOCAL_OPTIMAL)
      printf("\nSolution Status: Locally Optimal\n\n");
      else if (nStatus==LS_STATUS_INFEASIBLE)
      printf("\nSolution Status: Infeasible\n\n");
   }

 /* >>> Step 7 <<< Delete the LINDO environment */
   LSdeleteEnv(&pEnv);

  /* Wait until user presses the Enter key */
   printf("Press <Enter> ...");
   getchar();
}

“lindo.h”的一部分是:

/*********************************************************************
 * Structure Creation and Deletion Routines (4)                      *
 *********************************************************************/

 pLSenv CALLTYPE LScreateEnv(int     *pnErrorcode,
                             char    *pszPassword);

 pLSmodel CALLTYPE LScreateModel(pLSenv pEnv,
                             int     *pnErrorcode);

 int CALLTYPE LSdeleteEnv(pLSenv *pEnv);


 int CALLTYPE LSdeleteModel(pLSmodel *pModel);


 int CALLTYPE LSloadLicenseString(char *pszFname, char *pachLicense);

 void CALLTYPE LSgetVersionInfo(char *pachVernum, char    *pachBuildDate);

谢谢!


谢谢你们回答我的问题。正如您所建议的,我需要在编译时链接库。我已经获得了可执行文件:

gcc -o ex_addinst  ./ex_addinst.o -L/home/li/work/tools/lindo/lindoapi/bin/linux64 -m64 -llindo64  -lmosek64 -lconsub3 -lc -ldl -lm -lguide -lpthread -lsvml -limf -lirc

但是运行可执行文件ex_addinst时又出现了另一个问题:运行后:

./ex_addinst

来了:

./ex_addinst: error while loading shared libraries: liblindo64.so.6.0: cannot open shared object file: No such file or directory

棘手的是,liblindo64.so.6.0位于lib文件夹中,其中包含:

libconsub3.so  libirc.so          liblindojni.so        libmosek64.so.5.0  lindo.par
libguide.so    liblindo64.so      liblindojni.so.6.0.3  libsvml.so         placeholder
libimf.so      liblindo64.so.6.0  libmosek64.so         lindoapivars.sh    runlindo

我已经使用 liblindo64.so.6.0liblindo64.so

之间创建了符号链接
ln -sf liblindo64.so.6.0 liblindo64.so

但这没有帮助。

谁能告诉我这里出了什么问题吗?

(我不确定是否应该将这个问题放在新帖子中,但我认为目前最好遵循旧帖子)

c reference undefined
5个回答
21
投票

好吧,

lindo.h
包含了这些函数的原型,但是这些函数实际定义在哪里呢?如果它们位于另一个 C 文件中,您也需要编译该文件,并将两个目标文件链接在一起。

如果这些函数是另一个静态库的一部分,您需要告诉链接器将该库与您的目标文件链接起来。

如果它们是用共享库定义的,您可能可以让 g++ 在编译时仍然链接到它,并负责库加载等。否则,您需要在运行时加载库并引用来自图书馆。这篇关于共享库的动态加载的维基百科文章包含一些示例代码。


1
投票

尝试

g++ -Wall -o ex_addinst ex_addinst。c

而不是

g++ -Wall -o ex_addinst ex_addinst.o

您想要编译 .c 文件,而不是 .o 文件。


1
投票

您需要告诉 gcc 链接到包含您正在使用的 LS... 函数的库或目标文件。头文件告诉编译器如何调用它们,但链接器需要知道从哪里获取编译后的代码。


1
投票

undefined reference to ...
不是申报问题。编译器失败,因为它找不到与这些声明的函数相关的符号(对象)。
在您的情况下,您使用 Limbo API,并包含头文件,但您没有告诉编译器与库链接:这就是它找不到符号的原因。
编辑:我忘记了你说你是 Linux 新手的部分。要与库链接,您需要使用 g++ 的
-L
/
-l
选项。
man g++
总是值得一读,Limbo 的文档也应该如此。


0
投票

用于编写和添加头文件:

头文件

//arithmatic.h
#ifndef _ARITHMATIC_H
#define _ARITHMATIC_H

int two_num_sum(int x, int y);

#endif
//arithmatic.c
#include "arithmatic.h"

int two_num_sum(int x, int y){
 return x +y;
}

主文件:

#include<stdio.h>
#include "arithmatic.h"

int main(){
 int sum = two_num_sum(3, 4);
  
  printf("%d",sum);

 return 0

编译:您还需要编译

arithmatic.c
文件。

$ gcc arithmatic.c main.c -o sum_program
$./sum_program
© www.soinside.com 2019 - 2024. All rights reserved.