Makefile:体系结构x86_64的未定义符号

时间:2018-08-15 13:21:25

标签: c++ makefile compiler-errors

我正在使我的应用程序更具可移植性,以便在其他操作系统上运行。在XCode和Visual Studio中工作。但是,切换到Makefile会给我带来一些问题。

错误:

Undefined symbols for architecture x86_64:                                     
"AppObject::Instance()", referenced from:                        
  _main in main-329f6c.o                                                                                          
ld: symbol(s) not found for architecture x86_64

Main.cpp

#include <iostream>

#include "AppObject.hpp"

AppObject* _appObject;

int main()
{
    _appObject = AppObject::Instance();

    return 0;
}

AppObject.hpp

#ifndef  _APPOBJECT_HPP_
#define  _APPOBJECT_HPP_

#include <iostream>

class AppObject
{
    public:
        static AppObject* Instance();
        virtual ~AppObject() {}

        void initInstance();

    private:
        AppObject();
        AppObject(const AppObject&);
        AppObject& operator= (const AppObject&);
        static AppObject* pinstance; 
};

#endif // _APPOBJECT_HPP_

AppObject.cpp

#include "AppObject.hpp"

AppObject* AppObject::pinstance = 0;

AppObject::AppObject() {}

AppObject* AppObject::Instance()
{
    if (pinstance == 0)
    {
        pinstance = new AppObject;
        pinstance->initInstance();
    }

    return pinstance;
}

void AppObject::initInstance()
{
   .....
}

这有点配对,但我还看到其他一些警告:

warning: in-class initialization of non-static data member is a C++11 extension

有人可以帮助我理解我的错误吗,我的错误可以在XCode和VS中使用,但不能使用make?

Makefile

CXX=clang++
CXXFLAGS=-g -std=c++11 -Wall -pedantic -c

LDFLAGS=-L/usr/local/opt/sqlite/lib
CPPFLAGS=-I/usr/local/opt/sqlite/include

SOURCES=scraper/main.cpp scraper/AppObject.cpp scraper/SQLManager.cpp

OBJECTS=$(SOURCES:.cpp=.o)

EXECUTABLE=scraper

all: $(SOURCES) $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS) 
    $(CXX) $(LDFLAGS) $(OBJECTS) -o $@

.cpp.o:
    $(CXX) $(CXXFLAGS) $< -o $@

完整输出:

$ make
clang  scraper/main.cpp -o scraper/main.o
In file included from scraper/main.cpp:3:
In file included from scraper/AppObject.hpp:6:
scraper/SQLManager.hpp:44:30: warning: in-class initialization of non-    static data member is a C++11 extension
  [-Wc++11-extensions]
        bool _bDatabaseReady = false;
                         ^
scraper/SQLManager.hpp:46:29: warning: in-class initialization of non-    static data member is a C++11 extension
  [-Wc++11-extensions]
        std::string _dbFile = "popcorn.db3";
                        ^
scraper/SQLManager.hpp:47:29: warning: in-class initialization of non-    static data member is a C++11 extension
  [-Wc++11-extensions]
        std::string _dbName = "popcorn";
                        ^
scraper/SQLManager.hpp:49:24: warning: in-class initialization of non-    static data member is a C++11 extension
      [-Wc++11-extensions]
        int _dbVersion = 1; // increment this when database structure changes
                   ^
scraper/SQLManager.hpp:51:28: warning: in-class initialization of non-static data member is a C++11 extension
      [-Wc++11-extensions]
        int _dbVersionType = 0; // 0 = normal, 1 = free, 2 = lite
                       ^
5 warnings generated.
Undefined symbols for architecture x86_64:
  "AppObject::Instance()", referenced from:
      _main in main-4a5276.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see     invocation)
make: *** [scraper/main.o] Error 1

在收到回复的建议后更新了make输出:

$ make
clang++  scraper/main.cpp -o scraper/main.o
In file included from scraper/main.cpp:3:
In file included from scraper/AppObject.hpp:6:
scraper/SQLManager.hpp:44:30: warning: in-class initialization of non-    static data member is a C++11 extension [-Wc++11-extensions]
        bool _bDatabaseReady = false;
                         ^
scraper/SQLManager.hpp:46:29: warning: in-class initialization of non-    static data member is a C++11 extension [-Wc++11-extensions]
        std::string _dbFile = "popcorn.db3";
                        ^
scraper/SQLManager.hpp:47:29: warning: in-class initialization of non-    static data member is a C++11 extension [-Wc++11-extensions]
        std::string _dbName = "popcorn";
                        ^
scraper/SQLManager.hpp:49:24: warning: in-class initialization of non-    static data member is a C++11 extension [-Wc++11-extensions]
        int _dbVersion = 1; // increment this when database structure changes
                   ^
scraper/SQLManager.hpp:51:28: warning: in-class initialization of non-    static data member is a C++11 extension [-Wc++11-extensions]
        int _dbVersionType = 0; // 0 = normal, 1 = free, 2 = lite
                       ^
5 warnings generated.
Undefined symbols for architecture x86_64:
  "AppObject::Instance()", referenced from:
      _main in main-0e97bb.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see     invocation)
make: *** [scraper/main.o] Error 1

1 个答案:

答案 0 :(得分:0)

首先,clang C 的前端。尽管可以用来编译C ++代码,但它不会添加C ++所需的所有标头路径或链接程序库。使用clang++

第二,您显示的输出根本没有任何标志。

第三,也是导致链接器错误的原因,是您没有使用创建对象文件所需的-c标志。如果没有-c标志,您将告诉clang前端程序来构建和链接名为scraper/main.o可执行文件文件。