简体   繁体   中英

Linker error with g++

I an compiling my code with the following command:

g++ configuration_test.cpp -o configuration_test -lboost_unit_test_framework -I/root/target/include -ljsoncpp -L/root/target/lib/ -lboost_system -L/home/shubhada/Downloads/build/x86_64/lib -lconfiguration -L/home/shubhada/Downloads/build/x86_64/lib -ldbclient -L/home/shubhada/Downloads/build/x86_64/lib -ljsonparser -lboost_serialization

My code is:

#include <boost/test/unit_test.hpp>
#include <string.h>
#include <iostream>
#include <istream>
#include <sstream>
#include <fstream>
#include <json/json.h> // To access members of Json Parser.
#include <json/value.h>
#include <json/reader.h>
#include <cstdlib>
#include <pthread.h>
#include <vector>
#include "../../include/Constant.h"
#include "../src/Configuration.h"
#include "../../transport-endpoint/src/TransportEndpoint.h"
#include "../../dbclient/src/DbClient.h"
#include "../../json-parser/src/JsonParser.h"

using namespace std;
using namespace Rubicon;

#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE XXX_Service

BOOST_AUTO_TEST_SUITE(Configuration)
BOOST_AUTO_TEST_CASE(test_config_check_file_not_empty) {

    BOOST_TEST_MESSAGE( "test_config_check_file_not_empty begins");

    std::ostringstream buf;
    std::ifstream input ("sample.config.json");
    buf << input.rdbuf();
    std::string strConfigStr = buf.str();

    /*Check that the config file is not empty.*/
    BOOST_CHECK(strcmp(strConfigStr.c_str(),"") != 0);

    BOOST_TEST_MESSAGE("test_config_check_file_not_empty ends");
}

BOOST_AUTO_TEST_CASE(test_config_check_valid_json_str) {

    BOOST_TEST_MESSAGE( "test_config_check_valid_json_str begins");

    std::vector<Type> type;
    Json::ValueIterator jsonItrBegin, jsonItrEnd;

    std::ostringstream buf;
    std::ifstream input ("sample.config.json");
    buf << input.rdbuf();
    std::string strConfigStr = buf.str();

    Json::Reader jsonRead;
    Json::Value jsonValueRoot;

    /*Get the configuation object settings.*/
    odConfiguration *config = new odConfiguration();
    config->init(strConfigStr);

    /*Parse the configuration json string.*/
    jsonRead.parse(strConfigStr, jsonValueRoot, false);
    /*Check that the processor count is properly parsed.*/
    BOOST_CHECK(1 == config->getProcessorCount()); 
    /*Check that the worker count is properly parsed.*/
    BOOST_CHECK(1 == config->getWorkerCount());
    /*Check that data type is properly parsed.*/
    for (jsonItrBegin = jsonValueRoot.begin(), jsonItrEnd = jsonValueRoot.end(); jsonItrBegin != jsonItrEnd; ++jsonItrBegin) {
        if (strcmp("data-type", jsonItrBegin.memberName()) == 0) {
            for (unsigned i = 0; i < jsonItrBegin->size(); ++i) {
                const Json::Value & jsonVal = (*jsonItrBegin)[i];
                if(strcmp("tag", jsonVal.asCString()) == 0) {
                    type.push_back(TAG);
                } else if(strcmp("metatopic", jsonVal.asCString()) == 0) {
                    type.push_back(METATOPIC);
                } else if(strcmp("topic", jsonVal.asCString()) == 0) {
                    type.push_back(TOPIC);
                } else if(strcmp("classification", jsonVal.asCString()) == 0) {
                    type.push_back(CLASSIFICATION);
                }
            }
            /*Check that the data type is parsed properly.*/
            BOOST_CHECK(type == config->getDataType());
        }
    }
    BOOST_TEST_MESSAGE("test_config_check_valid_json_str ends");
}

BOOST_AUTO_TEST_CASE(test_transport_endpoint_config_check_valid_json_str) {

    BOOST_TEST_MESSAGE( "test_transport_endpoint_config_check_valid_json_str begins");

    std::ostringstream buf;
    std::ifstream input ("sample.config.json");
    buf << input.rdbuf();
    std::string strConfigStr = buf.str();

    /* Get the configuration object settings.*/
    odConfiguration *config = new odConfiguration();
    config->init(strConfigStr);

    odTransportEndpointConfig transportEndpointConfig = config->getTransportEndpointConfig();

    /*Check that the transport endpoint implementation is properly parsed.*/ 
    BOOST_CHECK(XYZ == transportEndpointConfig.getImpl());
    /*Check that the transport endpoint type is properly parsed.*/
    BOOST_CHECK(TE_SUBCRIBER == transportEndpointConfig.getType());
    /*Check that the transport endpoint uri is properly parsed.*/
    BOOST_CHECK(strcmp("tcp://127.0.0.1:8888", (transportEndpointConfig.getUri()).c_str())==0);
    /*Check that the transport endpoint topic is properly parsed.*/
    BOOST_CHECK(strcmp("XXX", (transportEndpointConfig.getTopic()).c_str())==0);
    BOOST_TEST_MESSAGE("test_transport_endpoint_config_check_valid_json_str ends");
}


BOOST_AUTO_TEST_CASE(test_db_client_config_check_valid_json_str) {

    BOOST_TEST_MESSAGE( "test_db_client_config_check_valid_json_str begins");

    std::ostringstream buf;
    std::ifstream input ("sample.config.json");
    buf << input.rdbuf();
    std::string strConfigStr = buf.str();

    odConfiguration *config = new odConfiguration();
    config->init(strConfigStr);

    /*Get the db client config from the configuration object*/
    odDbClientConfig dbClientConfig = config->getDbClientConfig();

    /*Check that the db client implementation is properly parsed.*/
    BOOST_CHECK(DB_AAAA == dbClientConfig.getImpl());
    /*Check that the db client  host is properly parsed.*/
    BOOST_CHECK(strcmp("127.0.0.1", dbClientConfig.getHost().c_str()) == 0);
    /*Check that the db client port is properly parsed.*/
    BOOST_CHECK(3000 == dbClientConfig.getPort());
    /*Check that the db client namespace is properly parsed.*/
    BOOST_CHECK(strcmp("test", dbClientConfig.getNamespace().c_str()) == 0);
    /*Check that the db client set is properly parsed.*/
    BOOST_CHECK(strcmp("test-set", dbClientConfig.getSet().c_str()) == 0);

    BOOST_TEST_MESSAGE("test_db_client_config_check_valid_json_str end");
}
BOOST_AUTO_TEST_SUITE_END()

I am getting the following errors:

/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 1 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 2 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 3 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 4 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 5 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 6 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 7 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 8 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 9 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 10 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 11 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 12 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 13 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 14 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 15 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 16 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 17 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 18 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 19 has invalid symbol index 21
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_line): relocation 0 has invalid symbol index 2
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status

I googled and found that people have suggested adding a main function to solve this. But after adding main function even if the errors don't show up but the object file created say configuration_test.o executes nothing. It does not execute any of the test cases.

Can somebody help me resolve this.

BOOST_TEST_DYN_LINK and BOOST_TEST_MODULE are option macros that influence the behavior of <boost/test/unit_test.hpp> . They have to be defined before the header is included, otherwise the code in it is unable to make use of them.

Move

#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE XXX_Service

to the top, before

#include <boost/test/unit_test.hpp>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM