如何在Linux上编译一个编译并运行良好的“C”应用程序?

时间:2023-01-19 23:44:36

I am trying to compile and run a 'C' application on Linux which is running perfectly on Solaris. I first tried to just copy the binary file compiled on Solaris and run on Linux, but that gave me an error saying cannot execute binary file. Hence I tried to first compile the code on Linux with the same Makefile which was used to compile it on Solaris. Contents of the Makefile are as follows:

我正在尝试编译并运行一个在Solaris上运行完美的“C”应用程序。我首先尝试复制在Solaris上编译的二进制文件并在Linux上运行,但是这给了我一个错误,说不能执行二进制文件。因此,我尝试首先在Linux上编译代码,并使用相同的Makefile在Solaris上编译它。Makefile的内容如下:

PROC=$(ORACLE_HOME)/bin/proc
CFLAGS:=$(CFLAGS) -DSOLARIS
PROCFLAGS:=$(PROCFLAGS) -DSOLARIS
HEADERS= $(HOME)
target = $(HOME)
CC=gcc

%.c :%.ec ; $(PROC) $(PROCFLAGS) \
    INCLUDE=/usr/topendurc/inc \
    iname=$< oname=$(@F)

%.o :%.c ; $(CC) -I$(HEADERS) -DORA_PROC -c $(CFLAGS) \
    -L /usr/local/lib -L ./ -I /usr/local/include $<



MAKEC= mv $(target)/$(@F) $(target)/$(@F).old; \
    $(CC) $(CFLAGS) -lnsl -lsocket -lm  $^ -L $(target) \
    -L $(ORACLE_HOME)/lib -l clntsh \
    -o $(target)/$(@F)

$(target)/%:%.o  $(CLIBFILES); $(MAKEC)
%:%.o  $(CLIBFILES); $(MAKEC)

all: rm_interface clean

rm_interface: lrfunc.o tcp.o trace.o global.o rmi.o license.o purge.o fetch_data.o

clean:
    -rm lrfunc.o tcp.o trace.o global.o rmi.o purge.o license.o fetch_data.o trace.c global.c rmi.c

By using the above Makefile I got errors in the code as mentioned below:

通过使用上面的Makefile,我在代码中有如下错误:

gcc -I/home/dev1o -DORA_PROC -c  -DSOLARIS \
-L /usr/local/lib -L ./ -I /usr/local/include global.c
/home/oracle/oracle/product/10.2.0/db_1/bin/proc  -DSOLARIS \
INCLUDE=/usr/topendurc/inc \
iname=rmi.ec oname=rmi.c

Pro*C/C++: Release 10.2.0.1.0 - Production on Thu Jan 16 13:19:57 2014

Copyright (c) 1982, 2007, Oracle.  All rights reserved.

System default option values taken from:     /home/oracle/oracle/product/10.2.0/db_1/precomp/admin/pcscfg.cfg

make: *** [rmi.c] Segmentation fault
make: *** Waiting for unfinished jobs....
global.c: In function `timeout_timer':
global.c:556: error: invalid application of `sizeof' to incomplete type `itimerval'
global.c:558: error: dereferencing pointer to incomplete type
global.c:559: error: dereferencing pointer to incomplete type
global.c:561: error: dereferencing pointer to incomplete type
global.c:562: error: dereferencing pointer to incomplete type
global.c:564: error: `ITIMER_REAL' undeclared (first use in this function)
global.c:564: error: (Each undeclared identifier is reported only once
global.c:564: error: for each function it appears in.)
make: *** [global.o] Error 1

UPDATE: Later I included a header file in the file global.ec and the errors got reduced to the following:

更新:稍后我在文件全局中包含了一个头文件。欧共体和错误被简化为:

/home/oracle/oracle/product/10.2.0/db_1/bin/proc  -DSOLARIS \
    INCLUDE=/usr/topendurc/inc \
    iname=global.ec oname=global.c
/home/oracle/oracle/product/10.2.0/db_1/bin/proc  -DSOLARIS \
    INCLUDE=/usr/topendurc/inc \
    iname=rmi.ec oname=rmi.c

Pro*C/C++: Release 10.2.0.1.0 - Production on Thu Jan 16 15:05:26 2014

Copyright (c) 1982, 2007, Oracle.  All rights reserved.

System default option values taken from:     /home/oracle/oracle/product/10.2.0/db_1/precomp/admin/pcscfg.cfg

make: *** [global.c] Segmentation fault
make: *** Waiting for unfinished jobs....
Pro*C/C++: Release 10.2.0.1.0 - Production on Thu Jan 16 15:05:26 2014

Copyright (c) 1982, 2007, Oracle.  All rights reserved.


System default option values taken from:     /home/oracle/oracle/product/10.2.0/db_1/precomp/admin/pcscfg.cfg

make: *** [rmi.c] Segmentation fault

Note: The errors are being reported in the .c file but they are actually .ec files.

注意:错误在.c文件中被报告,但它们实际上是.ec文件。

Simplified version of global.ec

的简化版本global.ec

#ifdef ORA_PROC
    #include "xxxoracle.h"
#endif

#include <stdlib.h>
#include <stdio.h>
#include <libgen.h>
#include <string.h>
#include <stropts.h>
#include <sys/time.h>
#include <ctype.h>
#include <signal.h>

#include <unistd.h> 

EXEC SQL INCLUDE sqlca;
#define MILLION 1000000

/*connect to oracle database */ 
int
access_database(void)
{   
    char str[200]="";
    EXEC SQL BEGIN DECLARE SECTION;
        char ospasswd = '/';
        char dbname[40];
    EXEC SQL END DECLARE SECTION;


    if (getenv("DBNAME") != 0)
        strcpy (dbname, (char *) getenv("DBNAME"));
    else
    {
        trace("Error ENV variable DBNAME not defined");
        return(1);
    }

    #ifdef ORA_PROC
        if (strlen(dbname) == strcspn(dbname, "/"))
            /* connect to database using Unix/OS password*/
            EXEC SQL connect :ospasswd using :dbname;
        else
            EXEC SQL connect :dbname;
        EXEC SQL ALTER SESSION SET NLS_DATE_FORMAT = 'YYYYMMDDHH24MISS';
    #endif
    if (sqlca.sqlcode)
    {
        sprintf(str, "access_database(): Error connecting to database\0");
        return(1);
    }
    else
    {
        sprintf(str, "access_database(): Connecting to database");
    }
    return(0);
}

Someone had asked a similar question here and the response that was accepted was to use GNU Autoconf. But I feel that in my case some changes in the Makefile or by adding some missing header files can make it work as everything is working on Solaris. I also found a similar post on * and people have suggested to use a debugger like GDB. Will using GDB help in my case?

有人在这里问了一个类似的问题,被接受的回答是使用GNU Autoconf。但是我觉得,在我的情况下,Makefile中的一些更改或者添加一些丢失的头文件可以使它工作,因为所有的东西都在Solaris上工作。我还在*上找到了一个类似的帖子,人们建议使用像GDB这样的调试器。在我的案例中,使用GDB会有帮助吗?

Please help me as I am very new to this.

请帮助我,因为我对这个很陌生。

1 个解决方案

#1


0  

Using GDB will not help. Your make process is experiencing a 'segmentation fault' error while trying to create the target file:global.c. Any *.ec file in the same directory of the Makefile will be used to create a corresponding *.c file thru the use of the Pro*C tool (as prescribed by the Makefile). This stage of building your rm_interface is definitely failing. Most likely, the global.c file is not created, or malformed, which means you will not have the required prerequisites to build your final executable, which means you have nothing to debug with GDB. You need to find out why the Pro*C tool is unable to create global.c from global.ec using this command:

使用GDB将于事无补。在创建目标文件时,您的make进程正在经历一个“分割错误”错误。任何*。在Makefile的相同目录下的ec文件将被用来创建相应的*。c文件通过使用Pro* c工具(由Makefile指定)。构建rm_interface的这个阶段肯定是失败的。最有可能的是,全球。c文件不是创建的,也不是格式错误的,这意味着您将没有必要的先决条件来构建最终的可执行文件,这意味着您没有任何可调试的GDB。您需要了解为什么Pro*C工具不能创建全局。c从全球。电子商务使用这个命令:

/home/oracle/oracle/product/10.2.0/db_1/bin/proc  -DSOLARIS \
    INCLUDE=/usr/topendurc/inc \
    iname=global.ec oname=global.c

To investigate this, please use the suggestions already listed in the comments to the original question:

为了研究这一点,请使用在评论中已经列出的建议:

  • Remove -DSOLARIS, suggested by ShinTakezou, OR replace with something more appropriate: -DLINUX, etc.
  • 删除-DSOLARIS,由ShinTakezou建议,或者用更合适的替代:-DLINUX,等等。
  • Familiarize yourself with the Pro*C tool and it's options/arguments: ProC/C++ programmers guide
  • 熟悉Pro*C工具和它的选项/参数:ProC/ c++程序员指南。

#1


0  

Using GDB will not help. Your make process is experiencing a 'segmentation fault' error while trying to create the target file:global.c. Any *.ec file in the same directory of the Makefile will be used to create a corresponding *.c file thru the use of the Pro*C tool (as prescribed by the Makefile). This stage of building your rm_interface is definitely failing. Most likely, the global.c file is not created, or malformed, which means you will not have the required prerequisites to build your final executable, which means you have nothing to debug with GDB. You need to find out why the Pro*C tool is unable to create global.c from global.ec using this command:

使用GDB将于事无补。在创建目标文件时,您的make进程正在经历一个“分割错误”错误。任何*。在Makefile的相同目录下的ec文件将被用来创建相应的*。c文件通过使用Pro* c工具(由Makefile指定)。构建rm_interface的这个阶段肯定是失败的。最有可能的是,全球。c文件不是创建的,也不是格式错误的,这意味着您将没有必要的先决条件来构建最终的可执行文件,这意味着您没有任何可调试的GDB。您需要了解为什么Pro*C工具不能创建全局。c从全球。电子商务使用这个命令:

/home/oracle/oracle/product/10.2.0/db_1/bin/proc  -DSOLARIS \
    INCLUDE=/usr/topendurc/inc \
    iname=global.ec oname=global.c

To investigate this, please use the suggestions already listed in the comments to the original question:

为了研究这一点,请使用在评论中已经列出的建议:

  • Remove -DSOLARIS, suggested by ShinTakezou, OR replace with something more appropriate: -DLINUX, etc.
  • 删除-DSOLARIS,由ShinTakezou建议,或者用更合适的替代:-DLINUX,等等。
  • Familiarize yourself with the Pro*C tool and it's options/arguments: ProC/C++ programmers guide
  • 熟悉Pro*C工具和它的选项/参数:ProC/ c++程序员指南。