Using automatic conversion in Pybind11 - c++

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.

Related

Using Legacy Header in C++20 Modules with Clang Compiler

I am trying to write some codes in c++20 standard. I write some simple code in c++ module way. And I write '#include <iostream>' because 'import <iostream>' can not be compiled in clang-15.0.2 compiler. Then I meet compile error.
The code is:
// md.ixx
module;
#include <iostream>
export module md;
export int func() {
return 114514;
}
// main.cpp
#include <iostream>
import md;
using namespace std;
int main() {
auto val = func();
cout<<val<<endl;
return 0;
}
Compile error is:
error: main.cpp:10:5: error: missing '#include <iostream>'; 'cout' must be declared before it is used
cout<<val<<endl;
^
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.32.31326\include\iostream:40:57: note: declaration here is not visible
__PURE_APPDOMAIN_GLOBAL extern _CRTDATA2_IMPORT ostream cout;
^
main.cpp:10:16: error: missing '#include <ostream>'; 'endl' must be declared before it is used
cout<<val<<endl;
^
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.32.31326\include\ostream:1000:51: note: declaration here is not visible
basic_ostream<_Elem, _Traits>& __CLRCALL_OR_CDECL endl(
^
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.32.31326\include\ostream:269:9: error: missing '#include <xiosbase>'; 'ios_base' must be declared before it is used
ios_base::iostate _State = ios_base::goodbit;
^
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.32.31326\include\xiosbase:170:28: note: declaration here is not visible
class _CRTIMP2_PURE_IMPORT ios_base : public _Iosb<int> { // base class for ios
And If I delete #include<iostream> in file md.ixx. It can be successfully compiled.
So is there any solution to using <iostream> in both main.cpp and md.ixx?
Thank you a lot.

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

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;
}

Resolving "found '{' at file scope (missing function header?)" in VS2010 C++

I am using Visual Studio 2010 Express and I am getting the following errors for the file test.h, which when compiled outputs:
test.h(4): error C2061: syntax error : identifier 'test'
test.h(4): error C2059: syntax error : ';'
test.h(4): error C2449: found '{' at file scope (missing function header?)
test.h(18): error C2059: syntax error : '}'
The file test.h is described as follows:
#ifndef TEST_H
#define TEST_H
class test {
int a;
int b;
public:
test(int a, int b) {
this->a = a;
this->b = b;
}
int add() {
return 0;
}
};
#endif
The other file in the VS2010 project is test.c which is:
#include "test.h"
int main(int argc, char** argv) {
return 0;
}
I have a tried of multitude of ways to resolve this problem. Even if I define test.h as follows:
class test{
};
I still receive the same set of errors.
I saw a similar problem
https://stackoverflow.com/questions/7798876/strange-errors-when-using-byte-pbyte-instead-of-char-char-in-vs2k10-wdk-envi
with no response.
I will be really grateful if someone could please point out how to resolve these errors.
Thanks,
The Microsoft compiler supports both C and C++ languages, but they are not the same and need to be treated differently (for example class is no keyword in C and thus ultimately causes the error your get). So it has to somehow "know" what kind of language (C or C++) it is dealing with when compiling a source file (and thus also processing the includes).
It thinks you are trying to compile a C language file (because it has the file extension .c), while you are actually using the C++ language. Rename your file to have one of the file extensions the Microsoft C/C++ compiler recognizes as C++: .cpp, .cxx or .cc.
Alternatively, if you cannot rename the file, you can also use the /Tp command line option of cl.exe to force it to treat the file as a C++ file (for completeness /Tc would force the C language).

Link error "LogonUser" compiling C++ program?

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