I'm still learning on using cmake and I don't really understand how I should work on it. I'm used to using Visual Studio and have it doing everything for me, so I'm trying to do it in a "Visual Studio" way... not sure if it's that's what it means though... cause when the cmake provided me a VS solution files, when I open it, each cpp becomes its own project... I'm kinda confused. But everything compiles okay... so I'm not sure if I'm doing it right.
I've made a simple project below using cmake and I wonder if this is the correct way to structure the files and folders using cmake.
~ the tree ~
- CMakeLists.txt
- main.cpp
- test/
---- CMakeLists.txt
---- component/
-------- CMakeLists.txt
-------- component.h
-------- component.cpp
---- core/
-------- CMakeLists.txt
-------- core.h
-------- core.cpp
---- interface/
-------- CMakeLists.txt
-------- interface.h
~ the files ~
CMakeLists.txt
cmake_minimum_required (VERSION 3.4.1)
project (Test)
add_subdirectory(test)
add_executable(Test main.cpp)
target_link_libraries(Test test_core)
main.cpp
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World" << endl;
int x;
cin >> x;
return 0;
}
test/CMakeLists.txt
cmake_minimum_required(VERSION 3.4.1)
add_subdirectory(component)
add_subdirectory(core)
test/component/CMakeLists.txt
cmake_minimum_required(VERSION 3.4.1)
include_directories(../interface)
add_library(test_component component.cpp)
test/component/component.h
#ifndef COMPONENT_H
#define COMPONENT_H
#include "../interface/interface.h"
class Component : public Interface
{
public:
Component();
void getMessage();
virtual ~Component();
};
#endif
test/component/component.cpp
#include "component.h"
Component::Component()
{
}
void Component::getMessage()
{
}
Component::~Component()
{
}
test/core/CMakeLists.txt
cmake_minimum_required(VERSION 3.4.1)
add_library(test_core core.cpp)
target_link_libraries(test_core test_component)
test/core/core.h
#ifndef CORE_H
#define CORE_H
#include <string>
#include "../component/component.h"
class Core
{
public:
Core();
virtual ~Core();
};
#endif
test/core/core.cpp
#include "core.h"
Core::Core()
{
Component component = {};
}
Core::~Core()
{
}
test/interface/interface.h
#ifndef INTERFACE_H
#define INTERFACE_H
class Interface
{
public:
virtual void getMessage() {}
};
#endif
Sorry for too much code.. I hope it's clear enough.