0

I'm making my app more portable to run on other OS's. Works in XCode and Visual Studio. However, switching to Makefiles is causing me some problems.

Error:

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()
{
   .....
}

This is paired down a bit but I am seeing some other warnings also of:

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

Can anyone help me understand my mistake and my this works in XCode and VS but not using 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 $@

Full make output:

$ 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

Updated make output after taking advice from replies:

$ 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
Jasmine
  • 15,375
  • 10
  • 30
  • 48

1 Answers1

0

To begin with, clang is the C frontend. While it can be used to compile C++ code, it will not add all header paths or linker libraries needed for C++. Use clang++.

Secondly, the output you show is without any flags at all.

Thirdly, and the reason for your linker error, is that you don't use the -c flag which is needed to create object files. Without the -c flag, you tell the clang frontend program to build and link an executable file named scraper/main.o.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • I fixed those and still the same errors. I updated the Makefile above to reflect – Jasmine Aug 15 '18 at 19:35
  • @Jason But you forgot to update the output you get from building. And if you get the exact same output, then you're not editing with the `Makefile` you're using to build. – Some programmer dude Aug 15 '18 at 20:02
  • hmm, I am editing the makefile I am using (doing so from a command-line). I am getting the same output, but I'll update to show it is now using `clang++` and `-c`. – Jasmine Aug 15 '18 at 21:29