Click here to Skip to main content
15,889,861 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have build a code and I have 2 cpp files. I did include in the header(.h) the functin and I add also add in the main cpp the "header.h", but one error comes with:

||=== Build: Debug in Ex2CppFILES (compiler: GNU GCC Compiler) ===|
obj\Debug\main.o||In function `Z7WndProcP6HWND__jjl@16':|
C:\Users\..............\main.cpp|14|undefined reference to `StaticWindow()'|
||error: ld returned 1 exit status|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

this is the code:

The main.cpp
C++
//Headers
#include <windows.h>
#include "resources.h"

HWND hwnd;

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
{
    switch(msg)
    {
    case WM_CREATE:
        {
            StaticWindow();
            break;
        }
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProcW(hwnd, msg, wp, lp);
    }
    return 0;
}
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR args, int nCmdShow)
{
    WNDCLASSW wc = {0};

    wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    wc.hInstance     = hInst;
    wc.lpfnWndProc   = WndProc;
    wc.lpszClassName = L"myWindowClass";

    if(!RegisterClassW(&wc))
        return -1;

    hwnd = CreateWindowW(L"myWindowClass", L"Ex2FilesC++", WS_OVERLAPPEDWINDOW,
                  350, 150, 700, 500, NULL, NULL, NULL, NULL);

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    MSG msg = {0};

    while(GetMessage(&msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return 0;
}


the resources.cpp
C++
#include <windows.h>
#include "resources.h"

void StaticWindow(HWND hwnd)
{
    CreateWindowW(L"static", L"Static window from resources.cpp", WS_CHILD | WS_VISIBLE,
                  10, 20, 150, 50, hwnd, NULL, NULL, NULL);
}


resources.h
C++
void StaticWindow();


What I have tried:

I'm using Code::Blocks ver 17.12
and C++ the language..

Please help. Thank you..!
Posted
Updated 23-Jun-20 22:05pm
v2

Sorry to contradict OriginalGriff, but you need to modify main.cpp thus:
C++
HWND hwnd;
extern void StaticWindow(HWND hwnd);  // StaticWindow is an external function
 
Share this answer
 
Comments
M@gelearn 23-Jun-20 23:36pm    
I did add extern but I get the same error..
Richard MacCutchan 23-Jun-20 23:42pm    
You need to add both source files to the build.
M@gelearn 23-Jun-20 23:52pm    
The both files are in the source.. Same error.. I do not understand what I'm doing wrong
M@gelearn 23-Jun-20 23:58pm    
oky doky.. done..
i didn't do this on WM_CRATE:{ StaticWindow();
break;
}
I forgot to add "hwnd" in StaticWindow(hwnd); ofcourse it doesn't work...
Thank you ..
Richard MacCutchan 24-Jun-20 0:20am    
My apologies, I should have noticed that.
Try adding extern to your resources.cpp definition of StaticWindow: Understanding "extern" keyword in C - GeeksforGeeks[^]
 
Share this answer
 
Here some insight: Creating a new project - CodeBlocks[^].
 
Share this answer
 
oky doky.. done..
i didn't do this on WM_CRATE:{ StaticWindow();
break;
}
I forgot to add "hwnd" in StaticWindow(hwnd); ofcourse it doesn't work...



Thank you all.. !!!!! :)
 
Share this answer
 
I do not see your RESOURCE.CPP being compiled(according to your log), which means it is not in your build chain. Add it and everything should work just fine.
 
Share this answer
 
Comments
Richard MacCutchan 24-Jun-20 4:17am    
See his comments to my Solution above.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900