Using std::vector with smart pointers - visual-studio-2010

i have MainWindow class, wich have the button.
When the button click, it opens new form, and this form need to put in vector.
I do code like this:
Class Action controls adding form in vector and class Header is a structure of vector.I need class bcz in future i want to close all forms in vector in one moment.
#pragma once
#include "MyForm.h"
#include <vector>
using namespace std;
ref class Header
{
public:
Client::MyForm ^x;
void Close(){ x->Close();}
};
class Action
{
public:
vector<Header> vec;
};
This code crashes, when i adding vector in class Action. And output is:
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(516): error C3699: '*' : cannot use this indirection on type 'Header'
1> compiler replacing '*' with '^' to continue parsing
1> C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\type_traits(572) : see reference to class template instantiation 'std::allocator<_Ty>' being compiled
1> with
1> [
1> _Ty=Header
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\vector(650) : see reference to class template instantiation 'std::is_empty<_Alloc>' being compiled
1> with
1> [
1> _Alloc=std::allocator<Header>
1> ]
1> c:\users\user\desktop\client\client\Header.h(17) : see reference to class template instantiation 'std::vector<Header,std::allocator<_Ty>>' being compiled
1> with
1> [
1> _Ty=Header
1> ]
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(517): error C3699: '*' : cannot use this indirection on type 'const Header'
1> compiler replacing '*' with '^' to continue parsing
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(521): error C3699: '&' : cannot use this indirection on type 'Header'
1> compiler replacing '&' with '^' to continue parsing
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(522): error C3699: '&' : cannot use this indirection on type 'const Header'
1> compiler replacing '&' with '^' to continue parsing
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(586): error C3699: '*' : cannot use this indirection on type 'Header'
1> compiler replacing '*' with '^' to continue parsing
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(591): error C3699: '*' : cannot use this indirection on type 'Header'
1> compiler replacing '*' with '^' to continue parsing
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(591): error C3699: '&' : cannot use this indirection on type 'const Header'
1> compiler replacing '&' with '^' to continue parsing
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(683): error C3699: '*' : cannot use this indirection on type 'Header'
1> compiler replacing '*' with '^' to continue parsing
1> C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(755) : see reference to class template instantiation 'std::allocator_traits<_Alloc>' being compiled
1> with
1> [
1> _Alloc=std::allocator<Header>
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\vector(443) : see reference to class template instantiation 'std::_Wrap_alloc<std::allocator<_Ty>>' being compiled
1> with
1> [
1> _Ty=Header
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\vector(579) : see reference to class template instantiation 'std::_Vec_base_types<_Ty,_Alloc>' being compiled
1> with
1> [
1> _Ty=Header
1> , _Alloc=std::allocator<Header>
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\vector(652) : see reference to class template instantiation 'std::_Vector_alloc<false,std::_Vec_base_types<_Ty,_Alloc>>' being compiled
1> with
1> [
1> _Ty=Header
1> , _Alloc=std::allocator<Header>
1> ]
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(684): error C3699: '*' : cannot use this indirection on type 'const Header'
1> compiler replacing '*' with '^' to continue parsing
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(763): error C3699: '&' : cannot use this indirection on type 'Header'
1> compiler replacing '&' with '^' to continue parsing
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(765): error C3699: '&' : cannot use this indirection on type 'const Header'
1> compiler replacing '&' with '^' to continue parsing
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(861): error C3699: '*' : cannot use this indirection on type 'Header'
1> compiler replacing '*' with '^' to continue parsing
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(105): error C3699: '*' : cannot use this indirection on type 'Header'
1> compiler replacing '*' with '^' to continue parsing
1> C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\vector(449) : see reference to class template instantiation 'std::_Is_simple_alloc<std::_Wrap_alloc<std::allocator<_Ty>>>' being compiled
1> with
1> [
1> _Ty=Header
1> ]
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(107): error C3699: '*' : cannot use this indirection on type 'const Header'
1> compiler replacing '*' with '^' to continue parsing
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(109): error C3699: '&' : cannot use this indirection on type 'Header'
1> compiler replacing '&' with '^' to continue parsing
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(111): error C3699: '&' : cannot use this indirection on type 'const Header'
1> compiler replacing '&' with '^' to continue parsing
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(122): error C3699: '*' : cannot use this indirection on type 'Header'
1> compiler replacing '*' with '^' to continue parsing
1> C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\vector(469) : see reference to class template instantiation 'std::_Simple_types<Header>' being compiled
1> C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\vector(580) : see reference to class template instantiation 'std::_Vector_val<std::_Simple_types<Header>>' being compiled
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(123): error C3699: '*' : cannot use this indirection on type 'const Header'
1> compiler replacing '*' with '^' to continue parsing
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(124): error C3699: '&' : cannot use this indirection on type 'Header'
1> compiler replacing '&' with '^' to continue parsing
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(125): error C3699: '&' : cannot use this indirection on type 'const Header'
1> compiler replacing '&' with '^' to continue parsing
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\vector(487): error C3265: cannot declare a managed '_Myfirst' in an unmanaged 'std::_Vector_val<std::_Simple_types<Header>>'
1> may not declare a global or static variable, or a member of a native type that refers to objects in the gc heap
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\vector(488): error C3265: cannot declare a managed '_Mylast' in an unmanaged 'std::_Vector_val<std::_Simple_types<Header>>'
1> may not declare a global or static variable, or a member of a native type that refers to objects in the gc heap
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\vector(489): error C3265: cannot declare a managed '_Myend' in an unmanaged 'std::_Vector_val<std::_Simple_types<Header>>'
1> may not declare a global or static variable, or a member of a native type that refers to objects in the gc heap
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\vector(704): error C3699: '&' : cannot use this indirection on type 'const Header'
1> compiler replacing '&' with '^' to continue parsing
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\vector(710): error C3699: '&' : cannot use this indirection on type 'const Header'
1> compiler replacing '&' with '^' to continue parsing
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\vector(797): error C3699: '*' : cannot use this indirection on type 'const Header'
1> compiler replacing '*' with '^' to continue parsing
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\vector(864): error C3699: '&&' : cannot use this indirection on type 'Header'
1> compiler replacing '&&' with '^' to continue parsing
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\vector(887): error C3699: '&&' : cannot use this indirection on type 'Header'
1> compiler replacing '&&' with '^' to continue parsing
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\vector(1118): error C3699: '&' : cannot use this indirection on type 'const Header'
1> compiler replacing '&' with '^' to continue parsing
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\vector(1242): error C3699: '&' : cannot use this indirection on type 'const Header'
1> compiler replacing '&' with '^' to continue parsing
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\vector(1320): error C3699: '&' : cannot use this indirection on type 'const Header'
1> compiler replacing '&' with '^' to continue parsing
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\vector(1326): error C3699: '&' : cannot use this indirection on type 'const Header'
1> compiler replacing '&' with '^' to continue parsing
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\vector(1332): error C3699: '&' : cannot use this indirection on type 'const Header'
1> compiler replacing '&' with '^' to continue parsing
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\vector(1581): error C3699: '*' : cannot use this indirection on type 'const Header'
1> compiler replacing '*' with '^' to continue parsing
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\vector(1652): error C3699: '&' : cannot use this indirection on type 'const Header'
1> compiler replacing '&' with '^' to continue parsing
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\vector(1745): error C3699: '*' : cannot use this indirection on type 'const Header'
1> compiler replacing '*' with '^' to continue parsing
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I think, that std::vector doesnt supports smart pointers like in VS (^) please help me, how can i fix it, i need to have vector of opened forms.

^ is not a 'smart pointer', it's a .Net managed reference. It cannot be put into C++ containers, only into .Net containers.
I'd replace the vector with a System::Collections::Generic::List.

Related

Where is __chkstk defined?

I'm currently writing a new frontend for LLVM, and I've come across a problem when using large arrays on the stack. Apparently the compiler inserts references to __chkstk in functions that have a large stack, which is fine, but results in a linker error:
lld-link: error: undefined symbol: __chkstk
I see the purpose of __chkstk, and I don't want to do workarounds, like implementing dummy functions, setting /Gs9999999... or any of the other workarounds I've seen on forum posts. Instead, I would like to actually just link to this function, wherever it may be. Actually, I've even removed my own code from the equation, and am just using clang and other llvm tools, and can show the following:
PS> cat hello.c
int main()
{
char buffer[10000000];
}
PS> clang -S -emit-llvm hello.c
PS> cat hello.ll ; ModuleID = 'hello.c'
source_filename = "hello.c"
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-windows-msvc19.29.30038"
; Function Attrs: noinline nounwind optnone uwtable
define dso_local i32 #main() #0 {
%1 = alloca [10000000 x i8], align 16
ret i32 0
}
attributes #0 = { noinline nounwind optnone uwtable "disable-tail-calls"="false" "frame-pointer"="none" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" "unsafe-fp-math"="false" "use-soft-float"="false" }
!llvm.module.flags = !{!0, !1}
!llvm.ident = !{!2}
!0 = !{i32 1, !"wchar_size", i32 2}
!1 = !{i32 7, !"PIC Level", i32 2}
!2 = !{!"clang version 12.0.1"}
PS> llc -o hello.obj --filetype=obj hello.ll
PS> lld-link /out:hello.exe /entry:main hello.obj lld-link: error: undefined symbol: __chkstk
>>> referenced by hello.obj:(main)
Actually, I get the same error when I use link.exe instead of lld-link.
error LNK2001: unresolved external symbol __chkstk
When I compile directly to an executable, clang hello.c -o hello.exe, this works just fine, so I suppose that under the hood, clang is doing something special with the linker options, or maybe it's just preventing insertion of the __chkstk symbol in the first place? I would generally like to take the same approach as clang if I don't have a good reason not to, but this area of clang is opaque to me.
So my question then, is what lib files (or other linker options) is clang doing here? Or how do I otherwise include the header/lib file which contains __chkstk?
After a lot of back and forth, I've finally figured it out, in what I think is the correct way. I was using just Windows SDK, which is not enough to build on Windows, you also need Visual Studio, or at least just the Build Tools. So as part of my toolchain installation, I now install both of these, and use these parameters to the linker:
/defaultlib:C:\Program Files (x86)\Windows Kits\10\Lib\${sdkVersion}\ucrt\${targetDepth}\ucrt.lib
and add "msvcrt.lib" and "libcmt.lib", located in C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\${msvcVersion}\lib\
I do not distribute any of these files myself, rather, I just run the installers for Windows SDK and the MSVC Build Tools, and then expect the correct lib files to exist in those locations.
One good hint I got, was to look at the verbose output of the clang command, clang hello.c -o hello.exe -v which shows the full set of arguments being sent to the linker, and gives a good template to start from.

How to pass linker options to `cl` via MSVS 2017 x64 command line?

I have a small program which I want to execute via the command line. I run it from inside the IDE and it runs fine. I copy the entire compilation command and paste it on the MSVS command prompt and I get unresolved symbol linker errors (I have done this before on some programs that didn't require passing of linker options).
I've read from the documentation that I need to specify /link linker-options (from here). But when I do so, I get other errors. Then as instructed I went here, which should say how I should specify the linker options. But it doesn't, it's just a reference of those for link.exe. Do I need to pipe the cl command to link.exe, do I need to execute it after cl? I cannot find an example either.
Just for testing, the simple program main.cpp is the following:
#include <iostream>
#include <Windows.h>
#include <string>
int main()
{
DWORD pid;
HWND hwnd = FindWindow(0, L"Calculator");
GetWindowThreadProcessId(hwnd, &pid);
if (hwnd) {
std::cout << "Window is open, id = " << pid;
}
else {
std::cout << "Window not found" << '\n';
}
system("Pause");
}
It works properly inside the IDE. Now, as I mentioned, I copy the complete commands from MSVS 2017 compiler section:/JMC /permissive- /we"4239" /GS /Zc:rvalueCast /W3 /Zc:wchar_t /ZI /Gm- /Od /Fd"x64\Debug\vc141.pdb" /Zc:inline /fp:precise /D "_DEBUG" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /errorReport:prompt /WX- /Zc:forScope /RTC1 /Gd /MDd /std:c++17 /FC /Fa"x64\Debug\" /EHsc /nologo /Fo"x64\Debug\" /Fp"x64\Debug\GetProcessByName.pch" /diagnostics:classic
and linker part: /OUT:"J:\nik\Documents\Visual_Studio_Projects\GetProcess\x64\Debug\GetProcessByName.exe" /MANIFEST /NXCOMPAT /PDB:"J:\nik\Documents\Visual_Studio_Projects\GetProcess\x64\Debug\GetProcessByName.pdb" /DYNAMICBASE "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib" "comdlg32.lib" "advapi32.lib" "shell32.lib" "ole32.lib" "oleaut32.lib" "uuid.lib" "odbc32.lib" "odbccp32.lib" /DEBUG /MACHINE:X64 /INCREMENTAL /PGD:"J:\nik\Documents\Visual_Studio_Projects\GetProcess\x64\Debug\GetProcessByName.pgd" /SUBSYSTEM:CONSOLE /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /ManifestFile:"x64\Debug\GetProcessByName.exe.intermediate.manifest" /ERRORREPORT:PROMPT /NOLOGO /TLBID:1
and as Mike said, I combine them as follows cl [compile-options] main.cpp /link [linker-options] and execute this command from the directory where main.cpp is located.
The output is:
c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.1
5.26726\include\xlocale(319): warning C4530: C++ exception handler used, but unw
ind semantics are not enabled. Specify /EHsc
C:\Users\nik\documents\Visual_Studio_Projects\GetProcess\GetProcessByName\m
ain.cpp : fatal error C1083: Cannot open compiler generated file: 'x64\Debug" /E
Hsc /nologo /Fox64\Debug".asm': Invalid argument
How do I properly "pass"/specify linker options to build the program from the command line and run it?
The error you are seeing is a shell parse error in the compilation options. So the command does not get as far as actually compiling, let alone linking, and if there are in fact going to be any problems arising from your linkage options, the question doesn't show us what they are.
The parse error we see is caused by the option:
/Fa"x64\Debug\"
The documentation of /Fa
stipulates that when the form:
/Fa directory\
is used, to specify an output directory for assembly listings, the trailing
\ must be present to distinguish this form of the option from:
/Fa filename
You have done that, and placed the directory name in quotes - "x64\Debug\".
When the Windows shell (cmd) with which you are executing your command
parses a commandline it treats \ within quotation "..." as an escape character.
Thus the trailing \ escapes the closing " of "x64\Debug\" and the directory\
parameter of /Fa is continued through subsequent options of the command until
finally the compiler identifies the form:
/Fa filename
and attempts, unsuccessfully, to open an assembly listing called:
'x64\Debug" /E Hsc /nologo /Fox64\Debug".asm'
You can avoid this parse error in any of the following ways:
Escape the backslashes in the quoted pathnames
Change:
/Fa"x64\Debug\"
to:
/Fa"x64\\Debug\\"
and similarly for other pathnames in your commandline options.
Remove the quotation marks
Change:
/Fa"x64\Debug\"
to:
/Fax64\Debug\
Quotations around pathnames are needed only if there are embedded spaces in
the pathnames, so that the shell will parse a single string instead of two or more.
You haven't got any embedded spaces in any of the pathnames in any of your
commandline options, so you could remove the quotes from all of them.
Use / instead of \ as the pathname separator
Change:
/Fa"x64\Debug\"
to:
/Fa"x64/Debug/"
and similarly for other pathnames in your commandline. Windows is the only
operating system to use \ as a pathname separator. Others use /, and Windows has
accepted both in recent versions.
If this results in any visual confusion between
/ used as a path separator ("x64/Debug/") and / used as an option-prefix (/Fa),
cl will also allow you to use unix-style options, e.g. -Fa instead of /Fa.
And of course you could also remove the quotation marks and use / as the path
separator.
If you use one of these solutions only to fix the parse error in:
/Fa"x64\Debug\"
then the same parse error will be provoked by the subsequent option:
/Fo"x64\Debug\"
So apply the chosen solution consistently to the whole commandline.

how can i add version info to the build .exe file using spld SICStus

I have the following batch program that is used to build my Prolog program.
set /p VERSION="Enter Version:"
call "c:\Program Files (x86)\SICStus Prolog VC12 4.3.2\bin\sicstus.exe" -l build_program.pl -a %VERSION%
call "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\vcvars32.bat"
call "C:\Program Files (x86)\SICStus Prolog VC12 4.3.2\bin\spld.exe" --output="fox_optimizer.exe" --static "fox_optimizer.sav"
output="my_program.exe" --static "my_program.sav" --resources=VERSIONINFO.rc
the VERSION variable is read from the user and sent as a flag to the build_program.pl that does this:
:-
nl,nl,nl,
prolog_flag(argv, Arguments),
Arguments = [VersionNumber|_],
atom_concat('fox_optimizer_',VersionNumber,FinalFileToCompile),
nl,
write('Trying to complile: '),
write(FinalFileToCompile),
nl,nl,nl,
compile(FinalFileToCompile),
save_program('my_program.sav'),
halt.
besically it builds the .sav file that is then used by spld to build into .exe file.
I want to add meta-data to the build .exe and i know that wjen using c++ you have to add a resource file so i tried this ..
output="my_program.exe" --static "my_program.sav" --resources=VERSIONINFO.rc
where VERSIONINFO.rc is:
VS_VERSION_INFO VERSIONINFO
FILEVERSION 1,0,0,1
PRODUCTVERSION 1,0,0,1
FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
FILEFLAGS 0x1L
#else
FILEFLAGS 0x0L
#endif
FILEOS 0x40004L
FILETYPE 0x2L
FILESUBTYPE 0x0L
BEGIN
BLOCK "StringFileInfo"
BEGIN
BLOCK "000904b0"
BEGIN
VALUE "CompanyName", "Britannica Knowledge Systems"
VALUE "FileDescription", "Fox Optimizer"
VALUE "FileVersion", "1.0.0.1"
VALUE "InternalName", "fox_optimizer.exe"
VALUE "LegalCopyright", "Copyright (C) 2016"
VALUE "OriginalFilename", "fox_optimizer.exe"
VALUE "ProductName", "Fox Optimizer"
VALUE "ProductVersion", "1.0.0.1"
END
END
BLOCK "VarFileInfo"
BEGIN
VALUE "Translation", 0x9, 1200
END
END
but when running spld i get the following error
cl : Command line warning D9024 : unrecognized source file type
'VERSIONINFO.rc', object file assumed VERSIONINFO.rc : fatal error
LNK1136: invalid or corrupt file
The spld option --resources has nothing to do with Windows resources. What you want is to compile the .rc file to a .res file (using the rc tool) and pass that .res file to the C compiler (via spld) together with the other object files. Something like the following:
call "rc VERSIONINFO.rc"
call "C:\Program Files (x86)\SICStus Prolog VC12 4.3.1\bin\spld.exe" -v --output="my_program.exe" --static "my_program.sav" VERSIONINFO.res
This will embed the Windows resource file in the executable created by spld.
I added the -v flag to turn on some verbose debug information from spld. This is useful when troubleshooting.

Escaping quotes for COMPILE_DEFINITIONS

Currently I'm working on a cmake script to compile generated C code. I'm trying to generate a Visual Studio 10 project. The code contains several #include statements which have to be preprocessed before compilition:
#indude INC_FILE
Before starting with CMake the code was compiled with a manually maintained Visual Studio project. I added a preprocessor definition to replace INC_FILE with the final include header:
INC_FILE="foo.h"
For my new CMak script I tried to add this statement to the COMPILE_DEFINITION variable:
set_property(TARGET ${TARGET_NAME} APPEND_STRING PROPERTY COMPILE_DEFINITION "INC_FILE="foo.h"")
Sadly this doesn't work because the quotes are removed. In the generated visual studio project I found the in the project file:
INC_FILE=foo.h
I tried to escape the quotes:
"INC_FILE=\"foo.h\""
INC_FILE=\"foo.h\"
"INC_FILE=\\"foo.h\\"
"INC_FILE="foo.h"
All the above mentions possibilites didn't work. Are there any other tricks in cmake to get a correct quotes escaping for my preprocessor definition?
Tested with cmake 2.8.12.1:
add_executable(foo foo.cpp)
target_compile_definitions(foo PUBLIC FOO_INCL="foo.hpp")
Note 1
You can use include option instead of file's name macro:
#if defined(FOO_INCLUDE_SOME_FILE)
# include "some_file.hpp"
#elif defined(FOO_INCLUDE_OTHER_FILE)
# include "other_file.hpp"
#endif
IMHO it looks much cleaner.
Note 2
If you have a bad time with special characters use configure_file command:
> cat some_file.hpp.in
#include "#FOO_INCLUDE_FILE#"
> cat CMakeLists.txt
set(FOO_INCLUDE_FILE "other_file.hpp")
configure_file(some_file.hpp.in ${foo_BINARY_DIR}/include/some_file.hpp #ONLY)

How to get printf warnings in Visual Studio 2005?

When providing the wrong number of arguments to printf():
printf("%s", "foo", "bar");
or when by providing arguments of the wrong type:
printf("%d", "foo");
gcc is able to warn about these mistakes:
$ gcc -Wformat printf_too_many_arguments.c
printf_warnings.c: In function `main':
printf_warnings.c:5: warning: too many arguments for format
printf_warnings.c:5: warning: too many arguments for format
$ gcc -Wformat printf_argument_of_wrong_type.c
printf_warnings.c: In function `main':
printf_warnings.c:5: warning: format `%d' expects type `int', but argument 2 has type `char *'
printf_warnings.c:5: warning: format `%d' expects type `int', but argument 2 has type `char *'
How to get such warnings with Visual Studio 2005?
-- dave
I use cppcheck (http://cppcheck.sourceforge.net/) when working with Visual Studio 2005 which detects mismatches between the number of parameters provided to printf/wprintf and the number of parameters required.
Unfortunately it doesn't check the types match, but it's a start.
Unfortunately MSVC/Visual Studio does not support this.
See also __attribute__((format(printf, 1, 2))) for MSVC?
You will need additional software to do this. Take a look at PC-Lint (http://www.gimpel.com/). It can find these kinds of errors (and much more [potential] errors as well).

Resources