Problem when compiling c++ Singleton

Asked

Viewed 254 times

0

When I’m 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 paramain' Connection. o: in function Connection::Connection()': /home/orion/Documentos/Workspace/AngoritmoTCC/Aplicacao-Tcc-Refazer/Connection.cpp:16: referência indefinida paravtable for Connection' Connection. o: in function Connection::getInstance()': /home/orion/Documentos/Workspace/AngoritmoTCC/Aplicacao-Tcc-Refazer/Connection.cpp:20: referência indefinida paraConnection:'sinstance' /home/Orion/Documents/Workspace/Angoritmotcc/Application-Tcc-Redo/Connection.cpp:21: undefined reference to operator new(unsigned long)' /home/orion/Documentos/Workspace/AngoritmoTCC/Aplicacao-Tcc-Refazer/Connection.cpp:21: referência indefinida paraConnection:'sinstance' /home/Orion/Documents/Workspace/Angoritmotcc/Application-Tcc-Redo/Connection.cpp:22: undefined reference to Connection::sinstance' /home/orion/Documentos/Workspace/AngoritmoTCC/Aplicacao-Tcc-Refazer/Connection.cpp:21: referência indefinida paraOperator 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

This way, I can’t compile, and I need to turn it into a file. So, 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;
}

File Connection. h

#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*

2 answers

1

Basically, you are trying to compile a file to become an executable. It is not being compiled for library (neither static nor dynamic), using an object file as an intermediate.

Analyzing the executed commands:

g++ Connection.cpp -c

Basically being told to the compiler g++ to do partial file compilation Connection.cpp, resulting therefore in the object file Connection.o.

cc Connection.o -o Connection

cc is usually a nickname for the gcc. When used to pass object files and no flag indicating the desired compile target, it generates a single executable file containing all the information of the object files passed as argument. The output is usually the file a.out, but while using -o Connection, you are directing the exit to Connection.

To the gcc (and g++), the executable must necessarily have a function main described in any of the included object 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 function main. When using the command cc Connection.o -o Connection, you wouldn’t be passing the function main, generating the error described above.

Since the code wasn’t posted, that’s as much as I can deduce based on past information.


I also recommend using the g++ to make the final build, so that the standard C++ library is also included for Runtime

  • I can pass the code if you like, but they are simple, a Singleton to be used in the player/Stage for robotic simulation. That is, the code is just this, a file . the to be reunited with others of the same extension in a file . Being passed to the player to serve as a system controller. If you can help me with the solution, I appreciate it. There is nothing special, really, it is only the Singleton same, it is to represent inside the simulation the communication that we would have through radio waves.

  • So it has no function main?

  • It is in this repository https://github.com/andersoney/AlgoritmoNovo.git

  • No, it’s a extern "C". that’s called by the Stage.

  • Sorry, to compile you will need to install the Stage pq needs the library of it. I can provide the code, but the installation, I do not know how to help because it varies from system to system.

  • Here’s how to ask a question with a minimal, verifiable and complete example: https://answall.com/help/mcve; minimum means you’ll have what it takes to reproduce your problem, verifiable means that others will be able to verify what happened and complete means that it has no external dependency, it is all in the example provided next to the question.

  • to check is simple. Comments extern "C" and uses a main in place.

  • I’ll raise the example like this.

  • @Andersoneyrodrigues , your question remains the same. Improve it with a mcve

  • Ready. I put examples

Show 5 more comments

0

I decided, the way the code looks is this.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.