Link error "LogonUser" compiling C++ program? - c++

Hey Folks i am trying to compile this C++ program:
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <Windows.h>
#include "Validate.h"
JNIEXPORT jstring JNICALL Java_Validate_takeInfo(JNIEnv *env, jobject obj,
jstring domain, jstring id, jstring idca, jstring password)
{
const char *nt_domain;
const char *nt_id;
const char *nt_idca;
const char *nt_password;
nt_domain = env->GetStringUTFChars(domain, NULL);
nt_id = env->GetStringUTFChars(id, NULL);
nt_idca= env->GetStringUTFChars(idca, NULL);
nt_password = env->GetStringUTFChars(password, NULL);
HANDLE hToken = 0;
char *otherString;
bool aut;
aut = LogonUser(nt_id, nt_domain, nt_password, LOGON32_LOGON_NETWORK,
LOGON32_PROVIDER_DEFAULT, &hToken );
if(aut)
{
otherString = "true";
}
else
{
otherString = "false";
}
jstring newString = env->NewStringUTF((const char*)otherString);
return newString;
}
int main()
{
return 0;
}
Using this command:
cl -I"c:\Program files\Java\jdk1.5.0_07\include"
-I"C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\Include"
-I"c:\program files\java\jdk1.5.0_07\include\win32"
-LD D:\JNI\%filename%.cpp -D:\JNI\Fe%filename%.dll -link
-LIBPATH:"C:\Program Files\Microsoft Visual Studio 8\VC\lib"
-LIBPATH:"C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\Lib"
However i always get the following error:
Validate.obj : error LNK2019: unresolved external symbol __imp__LogonUserA#24
referenced in function _Java_Validate_takeInfo#24
Validate.dll : fatal error LNK1120: 1 unresolved externals
I have probably tried a thousand different ways to compile playing with the LIBPATH switch.
-link -LIBPATH:"C:\Program Files\Microsoft Visual Studio 8\VC\lib";"C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\Lib"
and many others.
[Update] if i switch around the lib paths and put "\PlatformSDK\lib" before the "\VC\lib" switch i get this error:
LINK : fatal error LNK1104: cannot open file 'uuid.lib'
becuase it now cannot recognise the other libpath. Any idea? [/Update]
How do i declare multiple libpaths? is there something else causing this?
As always, thanks guys

MSDN says that LogonUser is in Advapi32.lib. It looks like the problem is that you're not including Advapi32.lib. LIBPATH affects where the linker searches for libraries, not what libraries the linker searches for, and nowhere are you telling the linker to search for Advapi32.dll.
On Visual C++ 2008, you should be able to do include Advapi32.lib by going under Project, Properties, Configuration Properties, Linker, Additional Dependencies. I'm not sure about other versions.)
From the command line, you should be able to just list Advapi32.lib as an additional file to be linked. Try this:
cl -I"c:\Program files\Java\jdk1.5.0_07\include"
-I"C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\Include"
-I"c:\program files\java\jdk1.5.0_07\include\win32"
-LD D:\JNI\%filename%.cpp -D:\JNI\Fe%filename%.dll -link
-LIBPATH:"C:\Program Files\Microsoft Visual Studio 8\VC\lib"
-LIBPATH:"C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\Lib"
Advapi32.lib

Related

C++ destroys itself by detecting compile errors in the standard library [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 1 year ago.
Improve this question
I can't build the project in the Debug configuration due to the fact that the compiler detects strange compile errors in the standard library. The project can be built in the Release configuration, due to the absence of _DEBUG, but I need to debug the project somehow. How can it be fixed?
Example (<atomic>):
#ifndef _INVALID_MEMORY_ORDER
#ifdef _DEBUG
#define _INVALID_MEMORY_ORDER _STL_REPORT_ERROR("Invalid memory order")
#else // ^^^ _DEBUG / !_DEBUG vvv
#define _INVALID_MEMORY_ORDER
#endif // _DEBUG
#endif // _INVALID_MEMORY_ORDER
...
inline void _Check_memory_order(const memory_order _Order) noexcept {
// check that _Order is a valid memory_order
if (static_cast<unsigned int>(_Order) > static_cast<unsigned int>(memory_order_seq_cst)) {
_INVALID_MEMORY_ORDER; // C2660 C2059 C2143
}
}
Output:
>------ Build started: Project: CMakeLists, Configuration: Debug ------
[1/158] Building C object externals\glfw\src\CMakeFiles\glfw.dir\input.c.obj
[2/158] Building C object externals\glfw\src\CMakeFiles\glfw.dir\egl_context.c.obj
[3/158] Building C object externals\glfw\src\CMakeFiles\glfw.dir\vulkan.c.obj
[4/158] Building C object externals\glfw\src\CMakeFiles\glfw.dir\null_init.c.obj
[5/158] Building C object externals\glfw\src\CMakeFiles\glfw.dir\init.c.obj
[6/158] Building C object externals\glfw\src\CMakeFiles\glfw.dir\monitor.c.obj
[7/158] Building C object externals\glfw\src\CMakeFiles\glfw.dir\osmesa_context.c.obj
[8/158] Building C object externals\glfw\src\CMakeFiles\glfw.dir\win32_joystick.c.obj
[9/158] Building C object externals\glfw\src\CMakeFiles\glfw.dir\context.c.obj
[10/158] Building C object CMakeFiles\glew.dir\externals\glew\src\glew.c.obj
[11/158] Building C object externals\glfw\src\CMakeFiles\glfw.dir\window.c.obj
[12/158] Building C object externals\glfw\src\CMakeFiles\glfw.dir\null_monitor.c.obj
[13/158] Building C object externals\glfw\src\CMakeFiles\glfw.dir\win32_module.c.obj
[14/158] Building C object externals\glfw\src\CMakeFiles\glfw.dir\null_joystick.c.obj
[15/158] Building C object externals\glfw\src\CMakeFiles\glfw.dir\win32_thread.c.obj
[16/158] Building C object externals\glfw\src\CMakeFiles\glfw.dir\null_window.c.obj
[17/158] Building C object externals\glfw\src\CMakeFiles\glfw.dir\platform.c.obj
[18/158] Building C object externals\glfw\src\CMakeFiles\glfw.dir\win32_time.c.obj
[19/158] Building RC object externals\glew\CMakeFiles\libglew_shared.dir\build\glew.rc.res
Microsoft (R) Windows (R) Resource Compiler Version 10.0.10011.16384
Copyright (C) Microsoft Corporation. All rights reserved.
[20/158] Building RC object externals\glew\CMakeFiles\libglew_static.dir\build\glew.rc.res
Microsoft (R) Windows (R) Resource Compiler Version 10.0.10011.16384
Copyright (C) Microsoft Corporation. All rights reserved.
[21/158] Building C object externals\glfw\src\CMakeFiles\glfw.dir\win32_init.c.obj
[22/158] Linking C static library glew.lib
[23/158] cmd.exe /C "cd /D C:\Users\maxiemar\source\repos\breakout\externals\openal-soft && "C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe" -D "GIT_EXECUTABLE=C:/Program Files/Git/cmd/git.exe" -D LIB_VERSION=1.21.1 -D LIB_VERSION_NUM=1,21,1,0 -D SRC=C:/Users/maxiemar/source/repos/breakout/externals/openal-soft/version.h.in -D DST=C:/Users/maxiemar/source/repos/breakout/build/vs/x64-Debug/externals/openal-soft/version.h -P C:/Users/maxiemar/source/repos/breakout/externals/openal-soft/version.cmake"
[24/158] Building CXX object externals\openal-soft\CMakeFiles\common.dir\common\almalloc.cpp.obj
[25/158] Building C object externals\glfw\src\CMakeFiles\glfw.dir\win32_monitor.c.obj
[26/158] Building C object externals\glfw\src\CMakeFiles\glfw.dir\wgl_context.c.obj
[27/158] Building C object externals\glfw\src\CMakeFiles\glfw.dir\win32_window.c.obj
[28/158] Building CXX object externals\openal-soft\CMakeFiles\common.dir\common\alstring.cpp.obj
[29/158] Linking C static library externals\glfw\src\glfw3.lib
[30/158] Building CXX object externals\openal-soft\CMakeFiles\common.dir\common\ringbuffer.cpp.obj
[31/158] Building C object externals\glew\CMakeFiles\libglew_shared.dir\src\glew.c.obj
[32/158] Building C object externals\glew\CMakeFiles\libglew_static.dir\src\glew.c.obj
[33/158] Building CXX object externals\openal-soft\CMakeFiles\common.dir\common\polyphase_resampler.cpp.obj
[34/158] Linking C static library externals\glew\lib\glewd.lib
[35/158] Building CXX object externals\openal-soft\CMakeFiles\common.dir\common\alcomplex.cpp.obj
[36/158] Linking C shared library externals\glew\bin\glew-sharedd.dll
[37/158] Building CXX object externals\openal-soft\CMakeFiles\common.dir\common\alfstream.cpp.obj
[38/158] Building CXX object externals\openal-soft\CMakeFiles\common.dir\common\dynload.cpp.obj
[39/158] Building CXX object externals\openal-soft\CMakeFiles\common.dir\common\threads.cpp.obj
[40/158] Building CXX object externals\openal-soft\CMakeFiles\common.dir\common\strutils.cpp.obj
[41/158] Linking CXX static library externals\openal-soft\common.lib
[42/158] Generating hrtf_default.h
[43/158] Building CXX object CMakeFiles\breakout.dir\src\graphics\Texture.cpp.obj
[44/158] Building CXX object CMakeFiles\breakout.dir\src\input\InputManager.cpp.obj
[45/158] Building CXX object CMakeFiles\breakout.dir\src\graphics\Shader.cpp.obj
[46/158] Building CXX object CMakeFiles\breakout.dir\src\utils\FileManager.cpp.obj
FAILED: CMakeFiles/breakout.dir/src/utils/FileManager.cpp.obj
C:\PROGRA~1\MICROS~4\2022\COMMUN~1\VC\Tools\MSVC\1430~1.307\bin\Hostx64\x64\cl.exe /nologo /TP -DGLEW_STATIC -IC:\Users\maxiemar\source\repos\breakout\externals\glew\include -IC:\Users\maxiemar\source\repos\breakout\externals\openal-soft\include -IC:\Users\maxiemar\source\repos\breakout\externals\glm -IC:\Users\maxiemar\source\repos\breakout\externals\stb -IC:\Users\maxiemar\source\repos\breakout\externals\glfw\include -IC:\Users\maxiemar\source\repos\breakout\externals\openal-soft\include\AL /DWIN32 /D_WINDOWS /W3 /GR /EHsc /MDd /Zi /Ob0 /Od /RTC1 -std:c++17 /showIncludes /FoCMakeFiles\breakout.dir\src\utils\FileManager.cpp.obj /FdCMakeFiles\breakout.dir\ /FS -c C:\Users\maxiemar\source\repos\breakout\src\utils\FileManager.cpp
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\include\atomic(206): error C2143: syntax error: missing ')' before 'string'
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\include\atomic(206): error C2660: '_invalid_parameter': function does not take 2 arguments
C:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\ucrt\corecrt.h(348): note: see declaration of '_invalid_parameter'
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\include\atomic(206): error C2143: syntax error: missing ';' before 'string'
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\include\atomic(206): error C2059: syntax error: ')'
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\include\atomic(221): error C2143: syntax error: missing ')' before 'string'
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\include\atomic(221): error C2660: '_invalid_parameter': function does not take 2 arguments
C:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\ucrt\corecrt.h(348): note: see declaration of '_invalid_parameter'
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\include\atomic(221): error C2143: syntax error: missing ';' before 'string'
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\include\atomic(221): error C2059: syntax error: ')'
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\include\atomic(237): error C2143: syntax error: missing ')' before 'string'
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\include\atomic(237): error C2660: '_invalid_parameter': function does not take 2 arguments
C:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\ucrt\corecrt.h(348): note: see declaration of '_invalid_parameter'
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\include\atomic(237): error C2143: syntax error: missing ';' before 'string'
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\include\atomic(237): error C2059: syntax error: ')'
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\include\atomic(297): error C2143: syntax error: missing ')' before 'string'
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\include\atomic(297): error C2660: '_invalid_parameter': function does not take 2 arguments
C:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\ucrt\corecrt.h(348): note: see declaration of '_invalid_parameter'
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\include\atomic(297): error C2143: syntax error: missing ';' before 'string'
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\include\atomic(297): error C2059: syntax error: ')'
[47/158] Building CXX object CMakeFiles\breakout.dir\src\Window.cpp.obj
[48/158] Building CXX object CMakeFiles\breakout.dir\src\graphics\SpriteRenderer.cpp.obj
[49/158] Building CXX object CMakeFiles\breakout.dir\src\ResourceManager.cpp.obj
[50/158] Building CXX object CMakeFiles\breakout.dir\src\game\GameObject.cpp.obj
[51/158] Building CXX object CMakeFiles\breakout.dir\src\graphics\ShaderProgram.cpp.obj
[52/158] Building CXX object CMakeFiles\breakout.dir\src\game\GameLevel.cpp.obj
[53/158] Building CXX object CMakeFiles\breakout.dir\src\Game.cpp.obj
C:\Users\maxiemar\source\repos\breakout\src\graphics\Font.h(6): warning C5208: unnamed class used in typedef name cannot declare members other than non-static data members, member enumerations, or member classes
C:\Users\maxiemar\source\repos\breakout\src\Game.cpp(65): warning C4267: '=': conversion from 'size_t' to 'GLint', possible loss of data
C:\Users\maxiemar\source\repos\breakout\src\Game.cpp(68): warning C4267: '=': conversion from 'size_t' to 'GLint', possible loss of data
[54/158] Building CXX object CMakeFiles\breakout.dir\src\game\Brick.cpp.obj
[55/158] Building CXX object CMakeFiles\breakout.dir\src\game\Ball.cpp.obj
ninja: build stopped: subcommand failed.
CMakeLists.txt:
cmake_minimum_required(VERSION 3.8)
project(breakout)
find_package(OpenGL REQUIRED)
set(CMAKE_CXX_STANDARD 17)
set(SOURCES src/main.cpp src/WindowManager.cpp src/WindowManager.h src/Window.cpp src/Window.h src/graphics/Shader.cpp src/graphics/Shader.h src/graphics/ShaderType.h src/graphics/ShaderProgram.cpp src/graphics/ShaderProgram.h src/utils/FileManager.cpp src/utils/FileManager.h src/graphics/Texture.cpp src/graphics/Texture.h src/ResourceManager.cpp src/ResourceManager.h src/input/InputManager.cpp src/input/InputManager.h src/input/EventHandlers.h src/Game.cpp src/Game.h src/game/GameState.h src/graphics/SpriteRenderer.cpp src/graphics/SpriteRenderer.h src/Singleton.h src/game/GameObject.cpp src/game/GameObject.h src/game/Brick.cpp src/game/Brick.h src/game/GameLevel.cpp src/game/GameLevel.h src/game/Player.cpp src/game/Player.h src/game/Ball.cpp src/game/Ball.h src/physics/Direction.h src/physics/Collision.h src/physics/CollisionDetector.cpp src/physics/CollisionDetector.h src/graphics/Particle.h src/graphics/ParticleEmitter.cpp src/graphics/ParticleEmitter.h src/graphics/PostProcessor.cpp src/graphics/PostProcessor.h src/graphics/PostProcessingEffect.h src/game/PowerUp.cpp src/game/PowerUp.h src/game/PowerUpType.h src/utils/Random.cpp src/utils/Random.h src/audio/AudioManager.h src/audio/AudioManager.cpp src/audio/AudioFile.h src/graphics/TextRenderer.h src/graphics/TextRenderer.cpp src/graphics/Font.h src/graphics/GlyphInfo.h src/AssetsLoader.cpp src/AssetsLoader.h)
# GLFW build configuration
set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
add_subdirectory(externals/glfw)
# end
# ------------------------
# GLEW build configuration
add_subdirectory(externals/glew)
include_directories(externals/glew/include)
add_library(glew STATIC externals/glew/src/glew.c)
add_definitions(-DGLEW_STATIC)
# end
# ------------------------
# OpenAL build configuration
add_subdirectory(externals/openal-soft)
include_directories(externals/openal-soft/include)
# end
# ------------------------
# Header-only libraries
include_directories(externals/glm)
include_directories(externals/stb)
# end
add_executable(breakout ${SOURCES})
target_link_libraries(breakout glfw glew OpenAL ${OPENGL_LIBRARY})
Update
In general, I can't find anything that could cause such errors.
FileManager.h:
#pragma once
#include "../audio/AudioFile.h"
#include <GL/glew.h>
#include <string>
#include <vector>
class FileManager {
public:
static std::string readAsText(const std::string& path);
static std::vector<unsigned char> readAsBinary(const std::string& path);
static unsigned char* readImage(const std::string& path,
GLint width,
GLint height,
GLint components,
bool flip = false);
static AudioFile readOggFile(const std::string& path);
};
AudioFile.h:
#pragma once
#include <memory>
struct AudioFile {
int channels;
int sampleRate;
int samples;
std::unique_ptr<short> data;
int getSampleCount() const {
return channels * samples;
}
};
FileManager.cpp:
#include "FileManager.h"
// STB_IMAGE_IMPLEMENTATION must be defined in *.c or *.cpp file (not in header)
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
#include <stb_vorbis.c>
#include <iostream>
#include <fstream>
#include <sstream>
std::string FileManager::readAsText(const std::string &path) {
std::string content = "";
std::ifstream file(path);
if (file.is_open()) {
std::stringstream stream;
// Read file's buffer contents into stream
stream << file.rdbuf();
file.close();
// Convert stream into string
content = stream.str();
} else {
std::cerr << "Unable to open text file " << path << std::endl;
}
return content;
}
std::vector<unsigned char> FileManager::readAsBinary(const std::string& path) {
std::ifstream file(path, std::ios::binary | std::ios::ate);
if (!file.is_open()) {
std::cerr << "Unable to open font file " << path << std::endl;
}
auto size = file.tellg();
auto bytes = std::vector<unsigned char>(size);
file.seekg(0, std::ios::beg);
file.read(reinterpret_cast<char*>(&bytes.front()), size);
file.close();
return bytes;
}
unsigned char* FileManager::readImage(const std::string& path,
GLint width,
GLint height,
GLint channels,
bool flip) {
stbi_set_flip_vertically_on_load(flip);
auto image = stbi_load(path.c_str(), &width, &height, &channels, 0);
if (image == nullptr) {
std::cerr << "Unable to open image file " << path << std::endl
<< "Reason: " << stbi_failure_reason() << std::endl;
}
return image;
}
AudioFile FileManager::readOggFile(const std::string& path) {
AudioFile audioFile;
short* output;
audioFile.samples = stb_vorbis_decode_filename(path.c_str(),
&audioFile.channels,
&audioFile.sampleRate,
&output);
audioFile.data = std::unique_ptr<short>(output);
return audioFile;
}
Indeed, the problem was in the stb_vorbis.c file, whose #define's conflict with the standard library. I discovered this by isolating from stb and then going to this file.
I was inspired by the solution here.
It was suitable to me to rearrange #include directives in such a way that stb_vorbis.c was at the end of the list. Now it is possible to build the entire project completely.
FileManager.cpp:
#include "FileManager.h"
#include <iostream>
#include <fstream>
#include <sstream>
// STB_IMAGE_IMPLEMENTATION must be defined in *.c or *.cpp file (not in header)
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
#define STB_VORBIS_HEADER_ONLY
#include <stb_vorbis.c>
#undef STB_VORBIS_HEADER_ONLY

(How) can a dll call a function that is implemented in its loading program?

Usually we load a dll on windows and call it's functions which is marked as __declspec(dllexport), but can I call a function which is implemented in the loaded program from a dll?
Firstly speaking this can be done on Linux, check the question I asked earlier.
I wrote a test program to test this:
CMakeList.txt (sorry I'm new to Windows programming and don't know how to use visual studio):
cmake_minimum_required(VERSION 3.16)
project(untitled4)
set(CMAKE_CXX_STANDARD 17)
add_library(lib1 SHARED lib1.cpp)
add_executable(untitled4 main.cpp)
main.cpp
#include "iostream"
#include "windows.h"
extern "C" {
// this is the function I want to get called from dll
__declspec(dllexport)
float get_e() {
return 2.71;
}
}
int main() {
auto *handler = LoadLibrary("lib1.dll");
if (!handler) {
std::cerr << ERROR_DELAY_LOAD_FAILED << std::endl;
exit(1);
}
auto p = (float (*)()) GetProcAddress(handler, "get_pi");
std::cout << p() << std::endl;
}
lib1.cpp:
#include "iostream"
extern "C" {
// implemented in main.cpp
__declspec(dllimport)
float get_e();
__declspec(dllexport)
float get_pi() {
std::cout << get_e() << std::endl; // comment this line will compile, just like the normal case
return 3.14;
}
}
The compile will fail when building lib1:
NMAKE : fatal error U1077: '"C:\Program Files\JetBrains\CLion 2020.1.1\bin\cmake\win\bin\cmake.exe"' : return code '0xffffffff'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.27.29110\bin\HostX86\x64\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.27.29110\bin\HostX86\x64\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.27.29110\bin\HostX86\x64\nmake.exe"' : return code '0x2'
LINK Pass 1: command "C:\PROGRA~2\MICROS~2\2019\BUILDT~1\VC\Tools\MSVC\1427~1.291\bin\Hostx86\x64\link.exe /nologo #CMakeFiles\lib1.dir\objects1.rsp /out:lib1.dll /implib:lib1.lib /pdb:C:\Users\derwe\CLionProjects\untitled\cmake-build-debug\lib1.pdb /dll /version:0.0 /machine:x64 /debug /INCREMENTAL kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:CMakeFiles\lib1.dir/intermediate.manifest CMakeFiles\lib1.dir/manifest.res" failed (exit code 1120) with the following output:
Creating library lib1.lib and object lib1.exp
lib1.cpp.obj : error LNK2019: unresolved external symbol __imp_get_e referenced in function get_pi
lib1.dll : fatal error LNK1120: 1 unresolved externals
Stop.
So can I do this on windows? That is to call a function in main from a dll?
P.S. Ideas of adding a registry function in dll and pass get_e through it is thanks but cannot be considered in my real case.
Since you are using late binding for your .dll, you might do the same thing for function defined in the executable. Just call GetProcAddress in the same way, using this process handle (as it is already in the address space). Here is some (pseudo) code:
auto proc = GetModuleHandle(nullptr);
auto get_e = reinterpret_cast<float (*) ()>(GetProcAddress(proc, "get_e"));

Using automatic conversion in Pybind11

I'm trying to take advantage of some C++ functions by calling them from Python. To do that, I was trying to construct a small demo function to show myself how python types are converted to C++ types. According to the Pybind11 documentation, if you include pybind11/stl.h in your header, automatic conversion should take place for many common types:
https://pybind11.readthedocs.io/en/stable/advanced/cast/stl.html
What is wrong with the following code?
my.cpp
#include <vector>
int add_these(std::vector<int> &v) {
int sum=0;
for (int i = 0; i < v.size(); ++i) {
sum += v[i];
}
return sum;
}
wrap.cpp
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <vector>
#include "my.cpp"
namespace py=pybind11;
PYBIND11_MODULE(python_example, m) {
m.def("addup", &add_these);
#ifdef VERSION_INFO
m.attr("__version__") = VERSION_INFO;
#else
m.attr("__version__") = "dev";
#endif
}
I have successfully compiled other demos that I've built, so I don't think it's an error in my compiling process. But compiling this demo I get this error:
wrap.cpp
creating C:\Users\scottjr1\AppData\Local\Temp\pip-req-build-wyi5ezw1\build\lib.win-amd64-3.7
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\bin\HostX86\x64\link.exe /nologo /INCREMENTAL:NO /LTCG /DLL /MANIFEST:EMBED,ID=2 /MANIFESTUAC:NO /LIBPATH:c:\users\scottjr1\appdata\python\python37\libs /LIBPATH:c:\users\scottjr1\appdata\python\python37\PCbuild\amd64 "/LIBPATH:C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\ATLMFC\lib\x64" "/LIBPATH:C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\lib\x64" "/LIBPATH:C:\Program Files (x86)\Windows Kits\NETFXSDK\4.6.1\lib\um\x64" "/LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.17763.0\ucrt\x64" "/LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.17763.0\um\x64" /EXPORT:PyInit_python_example build\temp.win-amd64-3.7\Release\src/my.obj build\temp.win-amd64-3.7\Release\src/wrap.obj /OUT:build\lib.win-amd64-3.7\python_example.cp37-win_amd64.pyd /IMPLIB:build\temp.win-amd64-3.7\Release\src\python_example.cp37-win_amd64.lib
wrap.obj : error LNK2005: "int __cdecl add_these(class std::vector<int,class std::allocator<int> > &)" (?add_these##YAHAEAV?$vector#HV?$allocator#H#std###std###Z) already defined in my.obj
Creating library build\temp.win-amd64-3.7\Release\src\python_example.cp37-win_amd64.lib and object build\temp.win-amd64-3.7\Release\src\python_example.cp37-win_amd64.exp
build\lib.win-amd64-3.7\python_example.cp37-win_amd64.pyd : fatal error LNK1169: one or more multiply defined symbols found
error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Professional\\VC\\Tools\\MSVC\\14.16.27023\\bin\\HostX86\\x64\\link.exe' failed with exit status 1169
The problem was simple: header guards don't work on .cpp files so the solution was to break up my.cpp into my.hpp and my.cpp files and include the my.hpp file in the wrap.cpp file.
Of the few demos I've done, this was only required for this demo so far. I'm not sure why breaking up the file is necessary for this demo but not others where I've included the .cpp files directly.
#include "my.cpp" is wrong. Replace with #include "my.h".
my.h should contain:
#include <vector>
int add_these(std::vector<int> const &v);
the declaration of your function.
and my.cpp should contain the definition:
#include "my.h"
int add_these(std::vector<int> const&v) {
int sum=0;
for (int i = 0; i < v.size(); ++i) {
sum += v[i];
}
return sum;
}
the error you are seeing is that you have two .cpp files that contain the definition of the function; that is not allowed in C++.
#include "my.cpp" copy pastes the content of my.cpp where the include directive is. This is not the same as import in python.
This didn't occur in other cases probably because you didn't link the cpp file; regardless including cpp files breaks convention, don't do it.

Compile errors when using C++ and bcrypt header

I'm trying to test Windows Bcrypt. I have a test program:
#include <bcrypt.h>
#include <iostream>
#include <string>
#pragma comment (lib, "bcrypt.lib")
int main(int argc, char* argv[])
{
return 0;
}
Attempting to compile it:
>cl.exe /DWINVER=0x0600 /TP /GR /EHsc bcrypt-test.cpp /link /out:bcrypt-test.exe
Microsoft (R) C/C++ Optimizing Compiler Version 19.00.24210 for x64
Copyright (C) Microsoft Corporation. All rights reserved.
bcrypt-test.cpp
C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\shared\bcrypt.h(39):
error C2059: syntax error: 'return'
C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\shared\bcrypt.h(40):
error C2143: syntax error: missing ';' before '*'
C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\shared\bcrypt.h(40):
error C4430: missing type specifier - int assumed. Note: C++ does not support d
efault-int
...
C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\shared\bcrypt.h(681)
: error C3646: 'cbKeyLength': unknown override specifier
C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\shared\bcrypt.h(681)
: fatal error C1003: error count exceeds 100; stopping compilation
I am using a Visual C++ x64 Build Tools command prompt. As I understand things, Bcrypt needs to target Vista or above. WINVER=0x0600 should satisfy the requirement. I found a similar question on the MSDN forums at bcrypt.h build error?, and it says to use a modern SDK. I believe the Windows Kit SDK should satisfy the requirement.
Why am I experiencing compile errors, and how do I fix it?
Line 39 in bcrypt.h is the first typedef below. The preamble, like copyright and header guards, were skipped for brevity.
#ifndef WINAPI
#define WINAPI __stdcall
#endif
#ifndef _NTDEF_
typedef _Return_type_success_(return >= 0) LONG NTSTATUS;
typedef NTSTATUS *PNTSTATUS;
#endif
#ifndef BCRYPT_SUCCESS
#define BCRYPT_SUCCESS(Status) (((NTSTATUS)(Status)) >= 0)
#endif
You are missing an include.
#include <Windows.h> // <- Added this
#include <bcrypt.h>
#include <iostream>
#include <string>
#pragma comment (lib, "bcrypt.lib")
int main()
{
return 0;
}

Linking problems: Qt5.5 32bit VS2013

I have been searching for an answer to this but no luck as yet.
Using Qt5.5 32bit, VS2013 on Win8 64bit
My .pro file contains this:
INCLUDEPATH += "C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\Include"
LIBS += -L"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\Lib" -ladvapi32
I have checked both folders and WinUser.h is in the Include and User32.lib is present in the Lib.
I have the following two functions in my code:
void suppressAnimations()
{
ANIMATIONINFO m_original_settings;
m_original_settings.cbSize = sizeof(m_original_settings);
if (::SystemParametersInfo(SPI_GETANIMATION, sizeof(m_original_settings), &m_original_settings, 0)) {
ANIMATIONINFO no_animation = { sizeof(no_animation), 0 };
::SystemParametersInfo(SPI_SETANIMATION, sizeof(no_animation), &no_animation, SPIF_UPDATEINIFILE | SPIF_SENDCHANGE);
}
}
AND
int BackgroundTaskManager::changeMonitorState(bool turnOn)
{
int lparam = 2;
if(turnOn) {
lparam = -1;
BackgroundTaskManager::MonitorIsSleeping = false;
} else {
lparam = 2;
BackgroundTaskManager::MonitorIsSleeping = true;
}
return SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM)lparam);
}
I use this exact same code with Qt 5.3.2 32bit VS2010 on Windows 7 64bit and it compiles with no problems. In my current configuration, I get linking errors on the ::SystemParametersInfo and the SendMessage parts of the functions and a bunch of warnings on the headers.
Warnings
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\Include\sal_supp.h:57: warning: C4005: '__useHeader' : macro redefinition
Linking Errors
main.obj:-1: error: LNK2019: unresolved external symbol __imp__SystemParametersInfoW#16 referenced in function "void __cdecl suppressAnimations(void)" (?suppressAnimations##YAXXZ)
backgroundtaskmanager.obj:-1: error: LNK2019: unresolved external symbol __imp__SendMessageW#16 referenced in function "private: int __thiscall BackgroundTaskManager::changeMonitorState(bool)" (?changeMonitorState#BackgroundTaskManager##AAEH_N#Z)
Another bit of information, I tried this also using Qt5.5 32bit VS2013 on Win7 64bit and also received the errors as above. Yet another bit of information, in Qt Creator, I can press F2 over either of the symbols above and it takes me to the correct header.
Could someone point me in the correct direction?
I knew it was something simple:
LIBS += -L"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\Lib" -ladvapi32 -luser32
I did not link the user32.lib.