-2

I had a problem in the CMake that was fixed thanks to mascoj's help. Now I have this I run the test of the SocketTests file: "Empty tests suite"

If I order inside the class, the test works. Here is the architecture of the project:

+-- CMakeLists.txt
+-- Serveur
|    +-- CMakeLists.txt
|    +-- Serveur.cpp
|    +-- Serveur.h
|    +-- Socket.cpp
|    +-- Socket.h
|
+-- Tests
|    +-- CMakeLists.txt
|    +-- main.cpp
|    +-- lib
|    +-- ServeurTests
|       +-- SocketTests.cpp

The different files : ./ CMakeLists.txt :

cmake_minimum_required(VERSION 3.10)
project(ServeurCheckIn)

set(CMAKE_CXX_STANDARD 14)
add_subdirectory(Serveur)
add_subdirectory(Tests)

Serveur/CMakeLists.txt:

cmake_minimum_required(VERSION 3.10)
project(ServeurCheckIn)

set(CMAKE_CXX_STANDARD 14)

add_library(ServeurCheckIn SHARED Serveur.cpp Serveur.h Socket.cpp Socket.h)

Serveur/Socket.h :

#include <sys/socket.h>
#include <netinet/in.h>

namespace Serveur
{
    class Socket
    {
        public:
            Socket(int domaine, int type, int protocole);

            int Domaine();
            int Type();
            int Protocole();

        private:
            int _domaine;
            int _type;
            int _protocole;
    };
}

Serveur/Socket.cpp:

#include "Socket.h"

using namespace Serveur;

Socket::Socket(int domaine, int type, int protocole) :
    _domaine(domaine), _type(type), _protocole(protocole)
{
}

int Socket::Domaine()
{
    return _domaine;
}

int Socket::Type()
{
    return _type;
}

int Socket::Protocole()
{
    return _protocole;
}

Tests/CMakeLists.txt:

cmake_minimum_required(VERSION 3.10)
project(Tests)

set(CMAKE_CXX_STANDARD 14)

add_subdirectory(lib/googletest-master)
include_directories(lib/googletest-master/googletest/include)
include_directories(lib/googletest-master/googlemock/include)

add_executable(Tests main.cpp ServeurTests/SocketTests.cpp )
target_link_libraries(Tests gtest gtest_main ServeurCheckIn)
enable_testing()

Tests/SocketTests.cpp:

#include <gtest/gtest.h>
#include "../../Serveur/Serveur.h"

using namespace Serveur;

class SocketTests : public testing::Test
{
    public:
        SocketTests() : _socket(AF_INET, SOCK_RAW, IPPROTO_IP)
        {
        }
    protected:
        Socket _socket;
};

TEST_F(SocketTests, CreateSocket_SocketIsCreated)
{
    ASSERT_EQ(1, 1);
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Doublon
  • 77
  • 1
  • 10
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Tsyvarev Jul 27 '18 at 16:28
  • It's probably not related to the problem, but you have a typo in `Serveur`. – pptaszni Jul 27 '18 at 16:35
  • Tsyvarev I think the problem is in the CMake as emphasized by mascoj. – Doublon Jul 27 '18 at 16:58
  • Ptaq666 Good point, but it's in French – Doublon Jul 27 '18 at 16:58
  • 1
    Yes, the problem was in `CMakeLists.txt`. Because you didn't link with a library you got "undefined reference" error. Exactly that is described in the referenced question. Would you ask "How to link a library in CMake", you would find another question, which describes that. As for current problem, in CMake tests are created with `add_test` command. I don't see call to this command in your code. – Tsyvarev Jul 27 '18 at 20:32

1 Answers1

1

Your Tests executable needs to be linked against your library you make with your other CMake.

target_link_libraries(Tests gtest gtest_main ServeurCheckIn)

Without it, the definitions for those functions will not be present at link time.

mascoj
  • 1,313
  • 14
  • 27