C++ 控制台应用程序未在 macOS 上的 Xcode 9.3 中编译

C++ console application is not compiling in Xcode 9.3 on macOS

提问人:Bijay Dixit 提问时间:2/6/2019 最后编辑:CœurBijay Dixit 更新时间:2/8/2019 访问量:82

问:

我正在使用 Xcode (9.3) 在 mac 上开发 C++ 控制台(命令行)应用程序。它编译和运行良好,但后来我做了一些代码更改,之后它显示了一些奇怪的编译时错误。喜欢 “/clang:-1:链接器命令失败,退出代码为 1(使用 -v 查看调用)”。

    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)

上面的文本显示在日志中。在分析日志后,我知道它没有定义“LJCPPBL”类的显示方法,但我已经正确定义了方法,并且头文件和 cpp 文件都包含在项目中。请帮帮我。

这是 LJCPPBL 头文件“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 */

这是LJCPPPBL头文件“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

这是我的主要.cpp文件

#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++ Xcode 静态库 链接器错误 未定义引用

评论

0赞 Botje 2/6/2019
您能否解释一下静态库是如何创建的,并验证您的目标是否与所述静态库链接?main
0赞 Mike Kinghan 2/6/2019
什么是未定义的引用/未解析的外部符号错误以及如何修复它?
0赞 Bijay Dixit 2/6/2019
@Botje抱歉,我的项目不是静态库,而是一个控制台(命令行工具)应用程序,我已经更新了问题
0赞 Bijay Dixit 2/6/2019
@MikeKinghan感谢您的回复,但我已经访问了这些链接,但我没有找到任何适合我的答案。请帮帮我。我是C++的新手
0赞 Botje 2/6/2019
如果你需要帮助,你肯定需要分享你的项目设置,因为看起来你的项目没有被编译和/或链接到。LJCPBBL.cppmain

答: 暂无答案