ex 26学习c没有执行的艰难方式

时间:2016-01-03 06:12:32

标签: c makefile

我在运行文件时遇到问题。它似乎编译文件,但它提供了有关加载共享库的错误。我附加了devpkg.c(还有其他多个文件,但我认为这是重要的文件)代码,Makefile和错误消息。

Makefile:

PREFIX?=/usr/local
CFLAGS=-g -Wall -I${PREFIX}/apr/include/apr-1  -I${PREFIX}/apr/include/apr-util-1
LDLIBS=-L${PREFIX}/apr/lib/ -lapr-1 -pthread -laprutil-1

all: devpkg

devpkg: bstrlib.o db.o shell.o commands.o

install: all
    install -d $(DESTDIR)/$(PREFIX)/bin/
    install devpkg $(DESTDIR)/$(PREFIX)/bin/

clean:
    rm -f *.o
    rm -f devpkg
    rm -rf *.dSYM

devpkg.c

#include <stdio.h>
#include <apr_general.h>
#include <apr_getopt.h>
#include <apr_strings.h>
#include <apr_lib.h>
#include <apr_pools.h>

#include "dbg.h"
#include "db.h"
#include "commands.h"

int main(int argc, const char const *argv[])
{
    apr_pool_t *p = NULL;
    apr_pool_initialize();
    apr_pool_create(&p, NULL);

    apr_getopt_t *opt;
    apr_status_t rv;

    char ch = '\0';
    const char *optarg = NULL;
    const char *config_opts = NULL;
    const char *install_opts = NULL;
    const char *make_opts = NULL;
    const char *url = NULL;
    enum CommandType request = COMMAND_NONE;


    rv = apr_getopt_init(&opt, p, argc, argv);

    while(apr_getopt(opt, "I:Lc:m:i:d:SF:B:", &ch, &optarg) == APR_SUCCESS) {
        switch (ch) {
            case 'I':
                request = COMMAND_INSTALL;
                url = optarg;
                break;

            case 'L':
                request = COMMAND_LIST;
                break;

            case 'c':
                config_opts = optarg;
                break;

            case 'm':
                make_opts = optarg;
                break;

            case 'i':
                install_opts = optarg;
                break;

            case 'S':
                request = COMMAND_INIT;
                break;

            case 'F':
                request = COMMAND_FETCH;
                url = optarg;
                break;

            case 'B':
                request = COMMAND_BUILD;
                url = optarg;
                break;
        }
    }

    switch(request) {
        case COMMAND_INSTALL:
            check(url, "You must at least give a URL.");
            Command_install(p, url, config_opts, make_opts, install_opts);
            break;

        case COMMAND_LIST:
            DB_list();
            break;

        case COMMAND_FETCH:
            check(url != NULL, "You must give a URL.");
            Command_fetch(p, url, 1);
            log_info("Downloaded to %s and in /tmp/", BUILD_DIR);
            break;

        case COMMAND_BUILD:
            check(url, "You must at least give a URL.");
            Command_build(p, url, config_opts, make_opts, install_opts);
            break;

        case COMMAND_INIT:
            rv = DB_init();
            check(rv == 0, "Failed to make the database.");
            break;

        default:
            sentinel("Invalid command given.");
    }


    return 0;

error:
    return 1;
}

错误讯息:

$ make clean;make
rm -f *.o
rm -f devpkg
rm -rf *.dSYM
cc -g -Wall -I/usr/local/apr/include/apr-1  -I/usr/local/apr/include/apr-util-1   -c -o bstrlib.o bstrlib.c
cc -g -Wall -I/usr/local/apr/include/apr-1  -I/usr/local/apr/include/apr-util-1   -c -o db.o db.c
cc -g -Wall -I/usr/local/apr/include/apr-1  -I/usr/local/apr/include/apr-util-1   -c -o shell.o shell.c
cc -g -Wall -I/usr/local/apr/include/apr-1  -I/usr/local/apr/include/apr-util-1   -c -o commands.o commands.c
cc -g -Wall -I/usr/local/apr/include/apr-1  -I/usr/local/apr/include/apr-util-1    devpkg.c bstrlib.o db.o shell.o commands.o  -L/usr/local/apr/lib/ -lapr-1 -pthread -laprutil-1 -o devpkg
$ ./devpkg
./devpkg: error while loading shared libraries: libapr-1.so.0: cannot open shared object file: No such file or directory

首先,为什么我在运行它时会出现此错误,为什么在编译时不会发生这种情况,是因为在运行时所有文件都调用此库并且无法在整个文件中共享不同的文件(但这对我来说似乎没有意义?如果您需要更多信息,请告诉我,我可以添加。

1 个答案:

答案 0 :(得分:2)

编译将源文件转换为二进制文件。此阶段不涉及库。因此,当您编译时,不会注意到丢失的库,并且您没有错误。

链接是第二阶段,您的二进制文件和您引用的库被放在一起形成一个可执行文件。在此阶段,如果找不到您引用的库,则无法构建可执行文件。这是你来到这里的错误。链接器找不到apr-1。查找文件夹/ usr / local / apr / lib /并查看库文件是否存在。如果没有,请将它们放在那里或更改make文件以告诉链接器实际位置。