0

I can't compile this simple project. I am using vs code.

// SampleApp.cpp

#include <Windows.h>
#include "SampleDLL.h"

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    HelloWorld();
    return 0;
}
// File: SampleDLL.h

#ifndef _SAMPLEDLL_H
#define _SAMPLEDLL_H

    #ifdef EXP_DLL
        #define EXPORT __declspec(dllexport)
    #else
        #define EXPORT __declspec(dllimport) 
    #endif

    extern EXPORT void HelloWorld();

#endif //_SAMPLEDLL_H
// SampleDLL.cpp

#include <Windows.h>
#define EXP_DLL
#include "SampleDLL.h"
BOOL WINAPI DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
    return TRUE;
}

void HelloWorld()
{
    MessageBox( NULL, TEXT("Hello World"), TEXT("In a DLL"), MB_OK);
}

Folder build source cmakelists.txt SampleApp.cpp SampleDLL.h SampleDLL.cpp

#cmakelists.txt

cmake_minimum_required(VERSION 3.26)
project(dinamica)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)

add_executable(programa_dinamica SampleApp.cpp)

add_library(sample SHARED SampleDLL.cpp)

target_link_libraries(programa_dinamica PRIVATE sample)

target_include_directories(programa_dinamica PUBLIC "${PROJECT_SOURCE_DIR}")

bash inputs:

Gabriel@DESKTOP-R12MDSF MINGW64 ~/Desktop/winmaindll/build
$ cmake ../source
$ cmake --build . --verbose

bash output:

MSBuild version 17.5.0+6f08c67f3 for .NET Framework
Compilación iniciada a las 23/3/2023 11:55:12.
Included response file: C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\
Bin\amd64\MSBuild.rsp

Proyecto "C:\Users\Gabriel\Desktop\winmaindll\build\ALL_BUILD.vcxproj" en nodo 1 (destinos prede
terminados).
El proyecto "C:\Users\Gabriel\Desktop\winmaindll\build\ALL_BUILD.vcxproj" (1) está compilando "C
:\Users\Gabriel\Desktop\winmaindll\build\ZERO_CHECK.vcxproj" (2) en el nodo 1 (destinos predeter 
minados).
InitializeBuildStatus:
  Se creará "x64\Debug\ZERO_CHECK\ZERO_CHECK.tlog\unsuccessfulbuild" porque se especificó "Alway 
  sCreate".
CustomBuild:
  Todas las salidas están actualizadas.
  1>Checking Build System
FinalizeBuildStatus:
  Se eliminará el archivo "x64\Debug\ZERO_CHECK\ZERO_CHECK.tlog\unsuccessfulbuild".
  Aplicando tarea Touch a "x64\Debug\ZERO_CHECK\ZERO_CHECK.tlog\ZERO_CHECK.lastbuildstate".
Compilación del proyecto terminada "C:\Users\Gabriel\Desktop\winmaindll\build\ZERO_CHECK.vcxproj
" (destinos predeterminados).

El proyecto "C:\Users\Gabriel\Desktop\winmaindll\build\ALL_BUILD.vcxproj" (1) está compilando "C
:\Users\Gabriel\Desktop\winmaindll\build\programa_dinamica.vcxproj" (3) en el nodo 1 (destinos p 
redeterminados).
El proyecto "C:\Users\Gabriel\Desktop\winmaindll\build\programa_dinamica.vcxproj" (3) está compi
lando "C:\Users\Gabriel\Desktop\winmaindll\build\sample.vcxproj" (4) en el nodo 1 (destinos pred 
eterminados).
InitializeBuildStatus:
  Se creará "sample.dir\Debug\sample.tlog\unsuccessfulbuild" porque se especificó "AlwaysCreate" 
  .
CustomBuild:
  Todas las salidas están actualizadas.
  1>Building Custom Rule C:/Users/Gabriel/Desktop/winmaindll/source/CMakeLists.txt
ClCompile:
  C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.35.32215\bin\HostX64\
  x64\CL.exe /c /Zi /nologo /W1 /WX- /diagnostics:column /Od /Ob0 /D _WINDLL /D _MBCS /D WIN32 / 
  D _WINDOWS /D "CMAKE_INTDIR=\"Debug\"" /D sample_EXPORTS /Gm- /EHsc /RTC1 /MDd /GS /fp:precise 
   /Zc:wchar_t /Zc:forScope /Zc:inline /Fo"sample.dir\Debug\\" /Fd"sample.dir\Debug\vc143.pdb" / 
  external:W1 /Gd /TP /errorReport:queue C:\Users\Gabriel\Desktop\winmaindll\source\SampleDLL.cp 
  p
  SampleDLL.cpp
Link:
  C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.35.32215\bin\HostX64\
  x64\link.exe /ERRORREPORT:QUEUE /OUT:"C:\Users\Gabriel\Desktop\winmaindll\build\Debug\sample.d 
  ll" /INCREMENTAL /ILK:"sample.dir\Debug\sample.ilk" /NOLOGO kernel32.lib user32.lib gdi32.lib  
  winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST / 
  MANIFESTUAC:"level='asInvoker' uiAccess='false'" /manifest:embed /DEBUG /PDB:"C:/Users/Gabriel 
  /Desktop/winmaindll/build/Debug/sample.pdb" /SUBSYSTEM:CONSOLE /TLBID:1 /DYNAMICBASE /NXCOMPAT 
   /IMPLIB:"C:/Users/Gabriel/Desktop/winmaindll/build/Debug/sample.lib" /MACHINE:X64  /machine:x 
  64 /DLL sample.dir\Debug\SampleDLL.obj
  sample.vcxproj -> C:\Users\Gabriel\Desktop\winmaindll\build\Debug\sample.dll
FinalizeBuildStatus:
  Se eliminará el archivo "sample.dir\Debug\sample.tlog\unsuccessfulbuild".
  Aplicando tarea Touch a "sample.dir\Debug\sample.tlog\sample.lastbuildstate".
Compilación del proyecto terminada "C:\Users\Gabriel\Desktop\winmaindll\build\sample.vcxproj" (d 
estinos predeterminados).

InitializeBuildStatus:
  Aplicando tarea Touch a "programa_dinamica.dir\Debug\programa.8E97E3D0.tlog\unsuccessfulbuild"
  .
CustomBuild:
  Todas las salidas están actualizadas.
  1>Building Custom Rule C:/Users/Gabriel/Desktop/winmaindll/source/CMakeLists.txt
ClCompile:
  C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.35.32215\bin\HostX64\
  x64\CL.exe /c /IC:\Users\Gabriel\Desktop\winmaindll\source /Zi /nologo /W1 /WX- /diagnostics:c 
  olumn /Od /Ob0 /D _MBCS /D WIN32 /D _WINDOWS /D "CMAKE_INTDIR=\"Debug\"" /Gm- /EHsc /RTC1 /MDd 
   /GS /fp:precise /Zc:wchar_t /Zc:forScope /Zc:inline /Fo"programa_dinamica.dir\Debug\\" /Fd"pr 
  ograma_dinamica.dir\Debug\vc143.pdb" /external:W1 /Gd /TP /errorReport:queue C:\Users\Gabriel\ 
  Desktop\winmaindll\source\SampleApp.cpp
  SampleApp.cpp
Link:
  C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.35.32215\bin\HostX64\
  x64\link.exe /ERRORREPORT:QUEUE /OUT:"C:\Users\Gabriel\Desktop\winmaindll\build\Debug\programa 
  _dinamica.exe" /INCREMENTAL /ILK:"programa_dinamica.dir\Debug\programa_dinamica.ilk" /NOLOGO D 
  ebug\sample.lib kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32. 
  lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTUAC:"level='asInvoker' uiAccess='fal 
  se'" /manifest:embed /DEBUG /PDB:"C:/Users/Gabriel/Desktop/winmaindll/build/Debug/programa_din 
  amica.pdb" /SUBSYSTEM:CONSOLE /TLBID:1 /DYNAMICBASE /NXCOMPAT /IMPLIB:"C:/Users/Gabriel/Deskto 
  p/winmaindll/build/Debug/programa_dinamica.lib" /MACHINE:X64  /machine:x64 programa_dinamica.d 
  ir\Debug\SampleApp.obj
MSVCRTD.lib(exe_main.obj) : error LNK2019: símbolo externo main sin resolver al que se hace refe
rencia en la función "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ) [C:\Users\Gabriel\Des 
ktop\winmaindll\build\programa_dinamica.vcxproj]
C:\Users\Gabriel\Desktop\winmaindll\build\Debug\programa_dinamica.exe : fatal error LNK1120: 1 e
xternos sin resolver [C:\Users\Gabriel\Desktop\winmaindll\build\programa_dinamica.vcxproj]       
Compilación del proyecto terminada "C:\Users\Gabriel\Desktop\winmaindll\build\programa_dinamica.
vcxproj" (destinos predeterminados) -- ERROR.

Compilación del proyecto terminada "C:\Users\Gabriel\Desktop\winmaindll\build\ALL_BUILD.vcxproj"
 (destinos predeterminados) -- ERROR.


ERROR al compilar.

"C:\Users\Gabriel\Desktop\winmaindll\build\ALL_BUILD.vcxproj" (destino predeterminado) (1) ->    
"C:\Users\Gabriel\Desktop\winmaindll\build\programa_dinamica.vcxproj" (destino predeterminado) ( 
3) ->
(Link destino) ->
  MSVCRTD.lib(exe_main.obj) : error LNK2019: símbolo externo main sin resolver al que se hace re 
ferencia en la función "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ) [C:\Users\Gabriel\D 
esktop\winmaindll\build\programa_dinamica.vcxproj]
  C:\Users\Gabriel\Desktop\winmaindll\build\Debug\programa_dinamica.exe : fatal error LNK1120: 1 
 externos sin resolver [C:\Users\Gabriel\Desktop\winmaindll\build\programa_dinamica.vcxproj]     

    0 Advertencia(s)
    2 Errores

Tiempo transcurrido 00:00:05.22
        
Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
Gabriel
  • 32
  • 5
  • 2
    Two things: Please don't tag multiple languages, only the one you're actually using. C and C++ are two *very* different languages. And secondly, all symbols beginning with an underscore and followed by an upper-case letter (like `_SAMPLEDLL_H`) are *reserved* and should not be defined by your code, neither as macros nor as variable or functions. – Some programmer dude Mar 23 '23 at 15:24
  • 1
    And please set your regional or locale settings so you get English error messages. This is an English-speaking site after all. – Some programmer dude Mar 23 '23 at 15:25
  • 1
    Change `add_executable(programa_dinamica SampleApp.cpp)` to `add_executable(programa_dinamica WIN32 SampleApp.cpp)` your code is building a console application. See the documentation [here](https://cmake.org/cmake/help/latest/command/add_executable.html) and [here](https://cmake.org/cmake/help/latest/prop_tgt/WIN32_EXECUTABLE.html#prop_tgt:WIN32_EXECUTABLE) – drescherjm Mar 23 '23 at 15:29
  • This doesn't address the question, but names that begin with an underscore followed by a capital letter (`_SAMPLEDLL_H`) are reserved for use by the implementation. Don't use them in your code. – Pete Becker Mar 23 '23 at 15:36
  • Also unrelated to the issue, but any export from a DLL should have an explicit [calling convention](https://learn.microsoft.com/en-us/cpp/cpp/calling-conventions) (such as `__stdcall`) specified. Otherwise the calling convention is subject to compiler options, and when consumers and authors disagree, bad things happen. – IInspectable Mar 23 '23 at 20:50

1 Answers1

3

Modify

add_executable(programa_dinamica SampleApp.cpp)

To

add_executable(programa_dinamica WIN32 SampleApp.cpp)

It will switch to using an entry function WinMain instead of main.

273K
  • 29,503
  • 10
  • 41
  • 64