Problem compiling singleton c ++

0

When trying to compile a singleton I always get the following return message:

  

g ++ -g -Wall pkg-config --cflags stage -fPIC pkg-config --libs stage -c -Wall Connection.cpp   cc Connection.o -o Connection   /usr/lib/gcc/x86_64-linux-gnu/6/../../../x86_64-linux-gnu/Scrt1.o: in function _start': (.text+0x20): referência indefinida para main '   Connection.o: in function Connection::Connection()': /home/orion/Documentos/Workspace/AngoritmoTCC/Aplicacao-Tcc-Refazer/Connection.cpp:16: referência indefinida para vtable for Connection '   Connection.o: in function Connection::getInstance()': /home/orion/Documentos/Workspace/AngoritmoTCC/Aplicacao-Tcc-Refazer/Connection.cpp:20: referência indefinida para Connection :: sinstance '   /home/orion/Documents/Workspace/AngoritmoTCC/Account-Tcc-Refazer/Connection.cpp:21: undefined reference to operator new(unsigned long)' /home/orion/Documentos/Workspace/AngoritmoTCC/Aplicacao-Tcc-Refazer/Connection.cpp:21: referência indefinida para Connection :: sinstance '   /home/orion/Documents/Workspace/AngoritmoTCC/Account-Tcc-Refacer/Connection.cpp:22: undefined reference to Connection::sinstance' /home/orion/Documentos/Workspace/AngoritmoTCC/Aplicacao-Tcc-Refazer/Connection.cpp:21: referência indefinida para operator delete (void *, unsigned long) '   Connection.o :( data.DW.ref .__ gxx_personality_v0 [DW.ref .__ gxx_personality_v0] + 0x0): undefined reference to '__gxx_personality_v0'   collect2: error: ld returned 1 exit status   : recipe for target 'Connection' failed   make: *** [Connection] Error 1

In this way, I can not compile, and need to transform into a .so file, to run using a library. Can someone help me?

Edited

Connection.cpp File

Connection::Connection() {
}

static Connection *Connection::getInstance() {
    if (!Connection::sinstance)
        Connection::sinstance = new Connection();
    return Connection::sinstance;
}

Connection.h File

#ifndef CONNECTION_H
#define CONNECTION_H

class Connection {
public:
    static Connection *getInstance();
    Connection();
    virtual ~Connection();


private:
    static Connection *sinstance;
};

#endif /* CONNECTION_H */

Main.cpp

int main(){
    ControllerPrincipal *controller = new ControllerPrincipal();
}

MaKeFIle

COMMON_DIR = ../common

run: all

all: coordination.so createScenario

createScenario: createScenario.cpp
    $(CXX) createScenario.cpp -o createScenario

coordination.so: ControllerPrincipal.o main.o Connection
    $(CXX) $(CXXFLAGS) $(LINKFLAGS) main.o ControllerPrincipal.o 
Connection.o -o coordination.so -shared

ControllerPrincipal.o: ControllerPrincipal.cpp ControllerPrincipal.h
    $(CXX) $(CXXFLAGS) $(LINKFLAGS) -c ControllerPrincipal.cpp

main.o: main.cpp
    $(CXX) $(CXXFLAGS) $(LINKFLAGS) -c main.cpp

Connection.o: Connection.cpp Connection.h
    $(CXX) $(CXXFLAGS) $(LINKFLAGS) -c -Wall Connection.cpp


clean:
    @rm -f *.o *.so  server createScenario

reset:
    rm -rf nRobos*
    
asked by anonymous 10.09.2017 / 00:48

2 answers

1

Basically, you are trying to compile a file to become an executable. It is not being compiled into a library (either static or dynamic), using an object file as an intermediary.

Analyzing the executed commands:

g++ Connection.cpp -c

Basically the g++ compiler is being told to compile part of the Connection.cpp file, resulting in the Connection.o object file.

cc Connection.o -o Connection

cc is usually a nickname for gcc . When used by passing object files and no flag indicative of the desired compilation target, it generates a single executable file containing all information from the passed object files as an argument. The output is usually the a.out file, however when using -o Connection , you are directing the output to Connection .

For gcc (and g++ ), the executable must necessarily have a main function described in any of the included objects files. The absence of this function causes an error when linking the executable.

In your case, Connection seems to be a class to be imported into the project, it does not seem to be where the main function lives. When using the cc Connection.o -o Connection command, you would not be passing the main function, generating the error described above.

Since the code has not been posted, this is the maximum I can deduce based on past information.

I also recommend using g++ to do the final compilation, so that the default C ++ library is also included for the runtime

    
10.09.2017 / 01:34
0

I decided, the way the code stays is this.

static Connection& getInstance(int numbRobot) {
    static Connection instance(numbRobot); // Guaranteed to be destroyed.
    // Instantiated on first use.
    return instance;
}
    
10.09.2017 / 06:02