1

I am developing a C++ console(Command Line) application on mac using Xcode (9.3). It was compiling and running fine but then I did some code changes and after that it's showing some strange compile time errors. like "/clang:-1: linker command failed with exit code 1 (use -v to see invocation)".

    Undefined symbols for architecture x86_64:
  "LJCPPBL_CORE::LJCPPBL::Initialize(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)", referenced from:
      _main in main.o
  "LJCPPBL_CORE::LJCPPBL::GetShortestPath(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, float*)", referenced from:
      _main in main.o
  "LJCPPBL_CORE::LJCPPBL::GetJson()", referenced from:
      _main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Above text is showing in log. After analysing the log i came to know that its showing methods of "LJCPPBL" class are not defined but i have defined the methods correctly and both the header file and cpp file are included in project. Please help me.

This is the LJCPPBL header file "LJCPPBL.hpp"

#ifndef LJCPPBL_hpp
#define LJCPPBL_hpp

#include <iostream>
#include "../Models/Models.hpp"
#include <list>
#include <string.h>

using namespace std;
using namespace LJCPPBL_Models;

namespace LJCPPBL_CORE {

 class LJCPPBL
    {
        public: static void Initialize(string clientAPIKEY);
        public: static string GetJson();
        public: static void SetJson(string strJson);
        public: static list<Destination> GetDestinationsList();
        public: static list<Point> GetShortestPath(string source, string destination, float* costOfPath);
    };
}

#endif /* LJCPPBL_hpp */

This is the Implementation of LJCPPBL header file "LJCPPBL.cpp"

#include <iostream>
#include "LJCPPBL.hpp"
#include "../Models/Models.hpp"
#include "../DAL/DataAccess.cpp"
#include "../Plugins/JsonParser/json.hpp"
#include "SPC.cpp"
#include "GlobalValues.cpp"
#include <list>

using namespace std;
using namespace LJCPPBL_CORE;
using namespace LJCPPBL_DAL;

namespace  LJCPPBL_CORE{
void LJCPPBL::Initialize(string clientAPIKEY){
        auto globalValues = new GlobalValues();
        LJCPPBLGlobalValues = *globalValues;
        LJCPPBLGlobalValues.ClientAPIKey = clientAPIKEY;
    }

    string LJCPPBL::GetJson()
    {
        LJCPPBLAPIDAL* objDAL = new LJCPPBLAPIDAL();
        string mapPointsAndPathJson = objDAL -> GetMapsAndPointsJSON();
        string mapDestinationJSON = objDAL -> GetMapDestinationsJSON();
        json jsonObj = {{"MapPointsAndPathJSON", mapPointsAndPathJson}, {"MapDestinationJSON", mapDestinationJSON} };
        return jsonObj.dump();
    }

    void LJCPPBL::SetJson(string strJson)
    {

    }

    list<Destination> LJCPPBL::GetDestinationsList()
    {
         LJCPPBLAPIDAL* objDAL = new LJCPPBLAPIDAL();
        return objDAL -> GetDestinations();
    }

    list<Point> LJCPPBL::GetShortestPath(string source, string destination, float* costOfPath)
    {
        return SPC::GetShortestPath(source, destination, costOfPath);
    }
}
//#endif

This is my main.cpp file

#include <iostream>
#include "Models/Models.hpp"
#include "Core/LJCPPBL.hpp"

using namespace std;
using namespace LJCPPBL_Models;
using namespace LJCPPBL_CORE;

int main(int argc, const char * argv[]) {

    //Destination* ds = new Destination();
    LJCPPBL::Initialize("APiKEY"); 
    string jsonstring = LJCPPBL::GetJson();
    string source = "SourceQuickLink";
    string destination = "DestinationQuickLink";
    float pathCost = -2;
    auto lstShortestPath = LJCPPBL::GetShortestPath(source, destination, &pathCost);

        if(lstShortestPath.size() == 1){
            cout << "\n No Path Exists From " << source << " To " << destination << "\n\n";
        }
        else{
            cout << "\nShortest Path From " << source << " To " << destination << " Is:\n\n";
            for (Point & point : lstShortestPath)
            {
                cout << point.Identifier << " (" << point.X << ", " << point.Y << ") ";

                if(point.Identifier != destination)
                    cout << " -> ";
                else
                    cout << "\n\nPath Cost Is: " << pathCost;
            }

            cout << "\n\nPath Cost Is: " << pathCost;
        }

    return 0;
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • Can you explain how your static library is created and verify that your `main` target is linked with said static library? – Botje Feb 06 '19 at 09:47
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Mike Kinghan Feb 06 '19 at 09:52
  • @Botje sorry, my project is not a static library its a Console(Command Line Tool) application, i have updated the question – Bijay Dixit Feb 06 '19 at 10:13
  • @MikeKinghan Thanks for your reply, but i have already visited those links but i didn't find any answer that works for me. Please help me. i am new to c++ – Bijay Dixit Feb 06 '19 at 10:15
  • If you want help you definitely need to share your project settings, as it looks like your `LJCPBBL.cpp` is not being compiled and/or linked to `main`. – Botje Feb 06 '19 at 10:22
  • @Botje Thanks for your reply. But How should i share my project settings?? – Bijay Dixit Feb 06 '19 at 11:52

0 Answers0