我的程序保持失败 - 加载共享库时出错

时间:2014-11-15 05:48:05

标签: c windows gcc dll cygwin

我编写了一个程序" run_coffee.c" 来实现 fork() exec()系统调用。它从根本上要求exec开始另一个过程" coffee" 多次通过" coffee.c" 构建。问题是我在Windows环境中的 cygwin64 上运行此程序,并且它一直失败并出现以下错误 - **

  加载共享库时出现

错误:?:无法打开共享库   file:没有这样的文件或目录

**

我还运行了cygcheck来查看是​​否满足了依赖性。这是输出 -

  

C:\ cygwin64 \ home \ Admin \ run_coffee.exe C:\ cygwin64 \ bin \ cygwin1.dll

     

C:\ Windows \ System32下\ KERNEL32.dll中

     

C:\ Windows \ System32下\ API-MS-的Win-芯RtlSupport-L1-1-0.dll

     

C:\ Windows \ system32 \ ntdll.dll C:\ Windows \ system32 \ KERNELBASE.dll

     

C:\ Windows \ System32下\ API-MS-的Win-芯ProcessThreads-L1-1-0.dll

     

C:\ Windows \ System32下\ API-MS-运核堆-L1-1-0.dll

     

C:\ Windows \ System32下\ API-MS-运核内存-L1-1-0.dll

     

C:\ Windows \ System32下\ API-MS-的Win-芯拉手L1-1-0.dll

     

C:\ Windows \ System32下\ API-MS-的Win-芯同步-L1-1-0.dll

     

C:\ Windows \ System32下\ API-MS-运核 - 文件-L1-1-0.dll

     

C:\ Windows \ System32下\ API-MS-运核-IO-L1-1-0.dll

     

C:\ Windows \ System32下\ API-MS-的Win-芯线程池-L1-1-0.dll

     

C:\ Windows \ System32下\ API-MS-的Win-芯LibraryLoader-L1-1-0.dll

     

C:\ Windows \ System32下\ API-MS-的Win-芯NamedPipe-L1-1-0.dll

     

C:\ Windows \ System32下\ API-MS-的Win-芯MISC-L1-1-0.dll

     

C:\ Windows \ System32下\ API-MS-的Win-芯SYSINFO-L1-1-0.dll

     

C:\ Windows \ System32下\ API-MS-的Win-芯定位-L1-1-0.dll

     

C:\ Windows \ System32下\ API-MS-的Win-芯ProcessEnvironment-L1-1-0.dll

     

C:\ Windows \ System32下\ API-MS-运核字符串-L1-1-0.dll

     

C:\ Windows \ System32下\ API-MS-运核 - 调试-L1-1-0.dll

     

C:\ Windows \ System32下\ API-MS-的Win-芯ErrorHandling中-L1-1-0.dll

     

C:\ Windows \ System32下\ API-MS-运核纤维-L1-1-0.dll

     

C:\ Windows \ System32下\ API-MS-的Win-芯的Util-L1-1-0.dll

     

C:\ Windows \ System32下\ API-MS-运核型材-L1-1-0.dll

     

C:\ Windows \ System32下\ API-MS-的Win-安全-BASE-L1-1-0.dll

没有错误或未满足的依赖性出现,所以我猜所有的依赖关系都得到满足。那导致这个问题的原因是什么?请帮忙。

以下是两个程序 -

coffee.c

#include<stdio.h>
#include<stdlib.h>

int main(int argc, char *argv[])
{
    char  *w = getenv("EXTRA");
    if (!w)
        w = getenv("FOOD");
    if (!w)
        w = argv[argc-1];
    char *c = getenv("EXTRA");
    if (!c)
        c = argv[argc-1];
    printf("%s with %s\n", c, w);
    return 0;
}

run_coffee.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

struct food_options
{
    char *food;
    char *extra;
};

int main()
{
    int i;
    char **env;
    env[0] = (char*)malloc(sizeof(char) * 20);
    env[1] = (char*)malloc(sizeof(char) * 20);
    env[2] = (char*)malloc(sizeof(char) * 20);
    struct food_options *opts = (struct food_options *)malloc(sizeof(struct food_options) * 3);
    opts[0].food = "coffee";
    opts[0].extra = "donuts";
    opts[1].food = "fish";
    opts[1].extra = "chips";
    opts[2].food = "kabab";
    opts[2].extra = "parantha";



    for (i = 0; i < 3; i++)
    {
        pid_t pid = fork();
        if (pid == -1)
        {
            fprintf(stderr, "Cannot fork process. Fatal Error %s\n", strerror(errno));
            return 1;
        }

        else if (!pid)
        {
            sprintf(env[0], "FOOD=%s", opts[0].food);
            sprintf(env[1], "EXTRA=%s", opts[0].extra);
            env[2] = NULL;
            if (execle("coffee.exe","coffee.exe",NULL,env) == -1)
            {
                fprintf(stderr, "Cannot execute coffee.exe. Error %s\n", strerror(errno));
            }
        }
    }
    free(opts);
    free(env[0]);
    free(env[1]);
    free(env[2]);
    return 0;
}

1 个答案:

答案 0 :(得分:0)

程序中存在内存错误,可能导致未定义的行为:您声明env是char *的数组,但是您没有初始化env。因此,env [0],env [1]和env [2]指向内存中的随机位置。当您执行sprintf(env [0],...)和sprintf(env [1],...)时,您正在将数据写入内存中的某个随机位置(其中env [0]和env [1]指向至)。这可能导致几乎任何事情发生,包括修改库的名称,使您无法加载它们。