【问题标题】:Make is not recognizing my library!Make 无法识别我的图书馆!
【发布时间】:2018-04-05 04:45:07
【问题描述】:

我正在尝试用 C++ 创建一个利用 rudeconfig 库的程序。

我运行 make,得到这个:

g++ -o Homework5_executable helloworld.o -lrudeconfig -L/home/j/je/jea160530/hw5/libs

/bin/ld: cannot find -lrudeconfig
collect2: error: ld returned 1 exit status
make: *** [Homework5_executable] Error 1

我知道这是因为 make 无法识别 rudeconfig 库,但是我已按照 rudeconfig 站点上的说明正确安装。

代码如下:

生成文件

#
# Set up info for C++ implicit rule
CXX = g++
CXXFLAGS = -Wall
CPPFLAGS = -I/home/012/j/je/jea160530/hw5/include 

#
# Set up any Linker Flags
LDFLAGS = -L/home/012/j/je/jea160530/hw5/libs

#
# Set up libraries needer for compilation
LDLIBS = -lrudeconfig

#
# We choose the project name. This is used in building the file name for the backup target
PROJECTNAME = JesseAlotto_Homework5

#
# We choose the source files to include and name the output
SRCS = helloworld.cc

#
# We choose the name of the executable to be created
EXEC = Homework5_executable

#
# NORMALLY DON'T NEED TO CHANGE ANYTHING BELOW HERE
# =================================================
#
OBJS = $(SRCS:cc=o)

all: $(EXEC)

clean:
    rm -f $(OBJS) *.d *~ \#* $(EXEC)

Makefile: $(SRCS:.cc=.d)





# Pattern for .d files.
# =====================

%.d:%.cc
    @echo Updating .d Dependency File
    @set -e; rm -f $@; \
    $(CXX) -MM $(CPPFLAGS) $< > $@.$$$$; \
    sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
    rm -f $@.$$$$



# This is a rule to link the files. Pretty standard
# ================================================


$(EXEC): $(OBJS)
    $(CXX) -o $(EXEC) $(OBJS) $(LDFLAGS) $(LDLIBS)
    @echo Program compiled succesfully!

#
# Backup Target
# =============

backup: clean
    @mkdir -p ~/backups; chmod 700 ~/backups
    @$(eval CURDIRNAME := $(bash pwd))
    @$(eval MKBKUPNAME := ~/backups/$(PROJECTNAME)-$(shell date +'%Y.%m.%d-%H:%M:%S').tar.gz)
    @echo
    @echo Writing Backup file to: $(MKBKUPNAME)
    @echo
    @tar -zcvf $(MKBKUPNAME) ./$(CURDIRNAME)
    @chmod 600 $(MKBKUPNAME)
    @echo
    @echo Done!
    #
# Include the dependency files
# ============================

-include $(SRCS:.cc=.d)

helloworld.cc

#include <string>
#include <iostream>
#include <fstream>
#include <tclap/CmdLine.h>
#include <map>
#include <stdlib.h>
#include <rude/config.h>

using namespace rude;

int main(int argc, char *argv[]){
    std::string nextLine;
    std::map<int, std::string> optionMap;


try{
    std::cout << "hello world!";


    //Command Line Variable
    TCLAP::CmdLine cmd("CS3377.002 Program 5", ' ', "1.0");

    //Switch Args
    TCLAP::SwitchArg daemonSwitch("d", "daemon", "Run in daemon mode (forks to run as a daemon).", cmd, false);

    //Unlabeled Value Args
    TCLAP::UnlabeledValueArg<std::string> infileArg("infile", "The name of the configuration file. Defaults to cs3376dirmond.conf", true, "cs3376dirmond.conf", "config filename", false);

    //Add leftover flags to cmdLine object
    cmd.add(infileArg);

    //Parse the command line
    cmd.parse(argc, argv);

    //Create an enumeratedlist for the mapping
    enum flags {DAEMON, INFILE};

    //Map keys and values to map

    if (daemonSwitch.getValue()){
        optionMap[DAEMON] = "1";
    }
    else{
        optionMap[DAEMON] = "0";
    }
    optionMap[INFILE] = infileArg.getValue();

    //Load input file
    std::ifstream inputFile;
    inputFile.open(optionMap[INFILE].c_str(), std::ios::in);

    if(!inputFile){
        std::cerr << "Error: no input file" << std::endl;
    }







    //============================================PARSE CONFIGURATION FILE==========================
    Config config;
    config.load("cs3376dirmond.conf");





    //==============================================================================================


    inputFile.close();
    return 0;

} catch (TCLAP::ArgException &e) //catch any exceptions
{ std::cerr << "error: " << e.error() << " for arg " << e.argId() << std::endl;}
}

【问题讨论】:

    标签: compilation makefile libraries c++ g++


    【解决方案1】:

    错误是由这个命令引起的:

    g++ -o Homework5_executable helloworld.o -lrudeconfig -L/home/j/je/jea160530/hw5/libs
    

    不是make 本身。

    错误意味着链接器在库搜索路径中没有找到librudeconfig.so。从您的 cmets 中得知,该库被命名为 rudeconfig.so,因此您需要指定

    LDLIBS = -l:rudeconfig.so
    

    而不是 -lrudeconfig(始终扩展为 librudeconfig.solibrudeconfig.a)。

    理想情况下,该库应安装为librudeconfig.so...

    【讨论】:

    • 不幸的是,交换这两个选项并不能解决我的问题。和以前一样的错误。我已经通过 pwd 命令的输出引用它来验证库的路径名是否正确。
    • 嗯...ls -l /home/j/je/jea160530/hw5/libs/librudeconfig.so 显示了什么?
    • pwd 显示 /home/012/j/je/jea160530/hw5 当我输入 ls -l /home/012/j/je/jea160530/hw5/libs 它显示以下内容“rudeconfig. lib" "rudeconfig.so -> ./rudeconfig.so.2.1" "rudeconfig.so.2.1"
    • 这很奇怪,这个库应该叫librudeconfig...,而不仅仅是rudeconfig...
    • @StephenKitt 来自linker manual,“-L searchir ...所有 -L 选项都适用于所有 -l 选项,无论选项出现的顺序如何。”
    猜你喜欢
    • 2016-02-05
    • 1970-01-01
    • 2011-05-06
    • 1970-01-01
    • 1970-01-01
    • 2019-10-24
    • 2016-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多