I found my solution.
The problem begins because the way the C++ compiler works sucks.
The whole idea to create multiple translation units per each cpp file is completely broken idea.
My solution to the problem is to force the compiler to work with only one translation unit.
This is my setup
main.cpp:
Code:
#include "win32helper.h" // header-only library. I may want to use it from here..
#include "MainGUI.cpp" // specific code for this project
#include "SettingsGUI.cpp" // specific code for this project
// Program Entry
int main() {
// Here I call to functions in namespaces MainGUI, SettingsGUI
SettingsGUI::CreateGUI();
while (SettingsGUI::mainGUI != NULL) {
SettingsGUI::ProcessGUI();
}
MainGUI::CreateMainGUI();
while (MainGUI::mainGUI != NULL) {
MainGUI::ProcessWindow();
}
// Just I want to call directly to my helper functions
HWND myOtherWindow = win32helper::CreateGUI(0, 0, 100, 100);
if (myOtherWindow) {
// Do here stuff
}
return 0;
}
You need to do the following for any cpp file other than main.cpp:
Screenshot by Lightshot
"excluded from build" option need to be set to "Yes" for these files.
I including them in main.cpp (as you can see)
win32helper.h:
Code:
#ifndef WIN32HELPER
#define WIN32HELPER
#include "windows.h"
// This is header-only library
namespace win32helper {
// Definition Section (Forward functions)
// Global variables
HWND activeHwnd = NULL;
// More variables go here
// Forward functions declarations just in case some function needs to call other in this namespace
bool SwitchToWindow();
HWND CreateGUI(int x, int y, int w, int h);
bool DeleteGUI(HWND hwnd);
// Implementation Section
// Functions
bool SwitchToWindow() {
// Some code to do stuff
return true;
}
HWND CreateGUI(int x, int y, int w, int h) {
// Some code that use win32 API
return NULL;
}
bool DeleteGUI(HWND hwnd) {
// Some logic to delete the GUI that use Win32 API
return true;
}
// More functions go here
}
#endif
MainGUI.h:
Code:
#include "win32helper.h"
namespace MainGUI {
// Global variables
enum enumGuiState {ACTIVE, NOT_ACTIVE};
HWND mainGUI = NULL;
int currentState = ACTIVE;
// Functions
bool CreateMainGUI();
void ProcessWindow();
}
MainGUI.cpp:
Code:
#include "MainGUI.h" // Definition Section (Forward functions)
// Implementation Section (This file is the implementation section)
namespace MainGUI {
bool CreateMainGUI() {
// Some code that use functions from win32helper.h
HWND window = win32helper::CreateGUI(0, 0, 300, 300);
return window != NULL;
}
void ProcessWindow() {
if (currentState == ACTIVE) {
// Do something
}
else {
// Do something else
}
}
}
SettingsGUI.h:
Code:
#include "win32helper.h"
namespace SettingsGUI {
// Global variables
enum enumSelectedTab { GENERAL, OTHER };
HWND mainGUI = NULL;
// Functions
void ProcessGUI();
void CreateGUI();
void CloseGUI();
}
SettingsGUI.cpp:
Code:
#include "SettingsGUI.h" // Definition Section (Forward functions)
// Implementation Section (This file is the implementation section)
namespace SettingsGUI {
// Functions
void ProcessGUI() {
// Do stuff
}
void CreateGUI() {
win32helper::CreateGUI(0, 0, 100, 100);
}
void CloseGUI() {
win32helper::DeleteGUI(mainGUI);
}
}
This is the way I could go with it.