Getting error message MSB6006 error code 2 with C/C++ - c++

I'm getting the following message trying to compile some simple code: MSB6006 "CL.exe" exited with code 2.
I'm trying to learn C++. I know some C. I understand the two are completely different languages. I include both tags because I get different results depending on how I try to compile the code.
For my own education, I'm trying to write a lexer. Mainly the problem seems to be with this function:
word scan(char** source)
{
word w;
w.lexeme[0] = '\0';
return w;
}
I get the same problem as this one MSB6006: "CL.exe" exited with code 2 but the answer doesn't apply in my case. I saw this question error MSB6006: "CL.exe" exited with code 2 which pointed to this question on the MSDN site They seem to indicate that small problems can cause this error code.
I have two files. One is a "driver" and the other is the lexer code. But I get the exact same results if I include everything in one file.
Here is the code for the driver:
#include "pch.h"
#include "Cl2aDLL.h"
void Cl2a(char argv1[], char argv2[])
{
char** source = NULL;
scan(source);
}
Here is the code for the header. I got the technique somewhere from a MSFT website:
#pragma once
#ifndef CL2ADLL__H__
#define CL2ADLL__H__
typedef struct {
char* lexeme;
}word;
#ifdef __cplusplus
extern "C" {
#endif
#ifdef CL2ADLL_EXPORTS
#define CL2ADLL_API __declspec(dllexport)
#else
#define CL2ADLL_API __declspec(dllimport)
#endif
CL2ADLL_API void Cl2a(char argv1[], char argv2[]);
word scan(char** source);
#ifdef __cplusplus
}
#endif
#endif // ! CL2ADLL__H__
Here is the code for the lexer:
// Error MSB6006 "CL.exe" exited with code 2.
#include "pch.h"
#include "Cl2aDLL.h"
/*
// when I comment out the following lines, it compiles and runs okay
word scan(char** source)
{
word w;
w.lexeme[0] = '\0';
return w;
}
*/
// if I only have the following, I get compile error if .cpp
word scan(char** source)
{
}
The strange thing is that if I compile as C code it compiles and runs okay. But if I try to compile as C++ I get the error message. If I uncomment out the first version of the scanner, I get the error message compiling as C or C++ either one.
Does anyone know of a change that can remove this error message?
Sorry for the long question, but I'm trying to give as clear a definition of the problem as I can. Because I can't figure out what could be wrong. TIA.
Update: I'm using VS 2019 Community Edition 16.1.1
Update 2: I got the same results with version 16.1.2. But trying the code in VS2017 Community Edition 15.9.12 showed the problem, as shown below.
Also I should have explained that all the above code was in a .dll file. The .dll code was run from a simple console application as follows:
#include "..\CL2aDLL\CL2aDLL.h"
int main(int argc, char* argv[])
{
char parm1[1 + 1] = "";
char parm2[1 + 1] = "";
if (argc == 1) {
Cl2aDLL(parm1, parm2);
}
else {
Cl2aDLL(argv[1], argv[2]);
}
return 0;
}

I've done some additional research.
Running the code in VS Community Edition 15.9.12 showed that the line w.lexeme[0] = '\0'; was trying to use an uninitialized pointer.
The corrected function is:
word scan(char** source)
{
word w;
w.lexeme = (char*)malloc(1); // <-- line added
w.lexeme[0] = '\0';
return w;
}
This compiles and runs okay.
However that still doesn't explain why this code gives the MSB6006 error:
word scan(char** source)
{
}
And this only happens when compiling as C++. It compiles and runs okay if compiling as C.
Update: I reported the problem to MSFT but it doesn't look liked they fixed the problem yet.

Related

E0167 error : C++ argument of type * is incompatible with parameter of type "FILE**"

i am very new to C and while working on examples in the book, i keep getting "C++ argument of type * is incompatible with parameter of type **" Error. I am using visual studio 2019 C++. Here is where i get the error when i use fopen_s:
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *inFile;
inFile = fopen_s("prices.dot","r"); /*Here is the line with an error*/
if (inFile == NULL)
{
printf("\nThis file does not existL");
printf("\nPlease make sure that this file currently exist");
exit(1);
}
printf("\nThe file has been succfully open for reading.");
return(0);
}
when i use "fopen" i get a warning which tells me to use fopen_s and when i do use fopen_s, i get the other error. I was wondering if i could get any help with this issue. Thank you!
If you google "MSDN fopen_s" and read the documentation on the Microsoft Developer Network, you will find that the function prototype is not identical to fopen():
errno_t fopen_s(
FILE** pFile,
const char *filename,
const char *mode
);
This means your code in that area would change to look like:
FILE *inFile;
errno_t errcode;
errcode = fopen_s(&inFile, "prices.dot","r");
if (errcode != 0) { /* do error handling, perhaps quit */ }
Alternately, you could continue to write old-style C code, and just use "fopen()" and at the top of your file, to shut MSVC compiler up, add the following #define:
#define _CRT_SECURE_NO_WARNINGS
Note, fopen_s() was added to the C 2011 standard, and is described in section K.3.5.2.1 -- meaning this is still portable C code on any modern C compiler.
Your book is probably older than 2011, and this function wasn't part of C at that time.

Visual Studio 2010 debugger points to wrong line

The debugger in Visual Studio 2010 is recently pointing at the wrong lines and/or skipping lines and I have no idea why this is. This is a CUDA project and only happens in CUDA files. I've noticed the following:
It always happens at the same part of the program.
The lines it points to are always the same, i.e. not random.
Putting extra code after the culprit lines changes which lines it points to.
It only happens in .cu-files. Moving the code to a .cpp-file does not recreate the problem.
What I have tried:
Clean and rebuilt the solution.
Install SP1 for MSVC10 and do all possible updates via Windows Updates
Set the compiler to not use optimizations in debug mode for both C/C++ and CUDA C/C++
Manually delete all created files and then rebuild from the solution folder.
Deleting the folder C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files
Recreating the solution only using the source files.
Disabling my extensions.
I've managed to reduce the code to the following which might reproduce the problem. Mind that this code has to be inside a .cu-file and most probably needs to be compiled with the cuda compiler for C/C++. Including boost is not really necessary, but this example does show what problems I'm having. A shorter example is at the back.
#include <boost/numeric/ublas/matrix.hpp>
using boost::numeric::ublas::matrix;
struct foo {
foo() : mat(NULL) {}
matrix<float>* mat;
};
bool func(foo *data) {
bool status; // <- skipped line
status = false;
if (status) {
std::cout << "test\n";
return (status); // <- error reported here
}
int size = data->mat->size1(); // instead of here
return status;
}
int main(int args, char* argv[]) {
func(NULL); // force error by passing NULL pointer
return 0;
}
Does anyone have any idea how to solve this or how this could be happening? It's pretty annoying having to debug this way.
Shorter example only showing the skipping lines. No external libraries necessary.
bool func() {
bool status; // <- skipped line
status = false;
return status;
}
int main(int args, char* argv[]) {
func();
return 0;
}
Since the program only contains CPU instructions and variable declarations of types that have no construction contain no instructions, the debugger will not stop there. It just executes instructions and then uses the debugging information that the compiler provided to find the relevant line of source code.

Function Call Stack in C++

I have tried the following links, from StackOverflow and other sites,[I tried, but it didn't helped me, so i can't avoid duplicating]
StackWalk64 on Windows - Get symbol name
How do you make StackWalk64() work successfully on x64?
http://www.codeproject.com/KB/threads/StackWalker.aspx
http://jpassing.com/2008/03/12/walking-the-stack-of-the-current-thread/
How to Log Stack Frames with Windows x64
...
But none of the Code worked for me.I'm new to Windows C++ environment and i can't get any of the above code to work.
I'm looking for a call stack format like,
FUNCTION_NAME_DEPTH_1 : _LINE_NUM__
FUNCTION_NAME_DEPTH_1 : _LINE_NUM__
FUNCTION_NAME_DEPTH_1 : _LINE_NUM__ ...
Just function name and line numbers.
My Environment:
Visual Studio 2010
SDK : v7.1
Windows 7 Pro SP1
It would be a lot simple if anyone post a header file,[there seems to be few available,but not working] which we can include in our cpp file and print the call stack with a call like 'PrintFunctionCallStack();' . BTW in Linux/Mac, it was a whole lot easier,i was able to get the call stack from backtrace and it was so simple that i did it myself in few mins. In Windows i've have been trying past two days, but no surprise at all.
Linux/Mac Stack Trace Code, i haven't yet demangled the symbol names.
#ifndef _STACKTRACE_H_
#define _STACKTRACE_H_
#include <stdio.h>
#include <stdlib.h>
#include <execinfo.h>
#include <cxxabi.h>
#include <iostream>
static inline void PrintStackTrace()
{
cout<<"##############################################\n";
unsigned int maxStackCount = 63;
void* addressList[maxStackCount+1];
int addrLen = backtrace(addressList, sizeof(addressList) / sizeof(void*));
if (addrLen == 0) {
cout<<"Empty Stack, Probably Corrupted it seems ###\n";
return;
}
char** symbolList = backtrace_symbols(addressList, addrLen);
for (int i = 1; i < addrLen; i++) // Skipped First, 'i' begins with '1'
{
cout<<"###: "<<symbolList[i]<<":###\n";
}
free(symbolList);
cout<<"##############################################\n";
}
#endif
If your environment is Visual Studio, you can insert a Tracepoint and input
$CALLSTACK
in its edit box, after checking Print a message.
To do it, right-click on the line you want and select Breakpoint > Insert Breakpoint (or alternatively, insert a breakpoint clicking on the left of the editor line you want, then select When Hit).
Then you will see a detailed report in the Output window, having file name, line number and function name. It served me to successfully discovered some memory leaks.

The weirdest bug (or something) in std::vector (or somewhere else). What's going on?

I've tried compiling this with Visual Studio 2012 RC and Intel C++ Compiler XE 12.1. I'd appreciate if you tried with some other compiler. See my comments in the code to really appreciate the weirdness of this bug. Does anyone know what's going on, and where should I file a bug report about this?
// File: NamedSameA.h
#pragma once
// File: NamedSameA.cpp
#include <vector>
#include "NamedSameA.h"
struct NamedSame // Rename this class to something else to make the program work
{
std::vector<int> data;
// Comment out the previous line or change
// the data type to int to make the program work
};
static NamedSame g_data; // Comment out this line to make the program work
// File: NamedSameB.h
#pragma once
void test();
// File: NamedSameB.cpp
#include <vector>
#include "NamedSameA.h"
#include "NamedSameB.h"
struct NamedSame
{
int data1; // Comment out this line to make the program work
std::vector<int> data2;
};
void test()
{
NamedSame namedSame;
namedSame.data2.assign(100, 42);
// The previous line produces the following runtime error:
// -------------------------------------------------------
// Debug Assertion Failed!
// Program: C:\Windows\system32\MSVCP110D.dll
// File: c:\program files (x86)\microsoft visual studio 11.0\vc\include\vector
// Line: 240
// Expression: vector iterators incompatible
}
By giving the same name to two different classes/structures you've violated the One Definition Rule. This results in undefined behavior, so any result is possible - including a crash.
I've found over the years that the more convinced I am that I've found a compiler bug, the more likely it is that my program has a fundamental flaw.

OutputDebugString causing inconsistent errors

I have a problem that appears and disappears for mysterious reasons. A while back when I started the project I've found a fairly handy function that allows debug window output in VS2010. It worked great for a while.
Now it displays errors inconsistently. That means that sometimes the code will compile, sometimes it does not, and I can't find out why with code below causing the error. It seems almost random. Press compile, error, press compile again without changing anything sometimes error sometimes fine.
This is what it looks like with the error:
http://clip2net.com/clip/m0/1332710747-clip-29kb.png
and without:
http://clip2net.com/clip/m0/1332737362-clip-40kb.png
The culprit is OutputDebugString(buf);
Error doesn't occur with that line commented out.
I am looking to solve this problem, i simply need a way to output text into debug window (output), and am looking for a simple, stable solution. Or perhaps there is a way to make this function work. I am kind of stuck.
I would appreciate it if you could share how you do it.
The code is:
#pragma once
#ifndef _XDEBUG_H_
#define _XDEBUG_H_
#include <stdio.h>
#include <stdarg.h>
#include <ctype.h>
class XDebug
{
public:
static void __cdecl odprintf(const wchar_t *format, ...){
wchar_t buf[4096], *p = buf;
va_list args;
int n;
va_start(args, format);
n = _vsnwprintf(p, sizeof buf - 3, format, args); // buf-3 is room for CR/LF/NUL
va_end(args);
p += (n < 0) ? sizeof buf - 3 : n;
while ( p > buf && isspace(p[-1]) )
*--p = '\0';
*p++ = '\r';
*p++ = '\n';
*p = '\0';
OutputDebugString(buf);
}
};
#endif
OutputDebugString is defined in Windows.h. You need to include that header to be able to use that function.
It looks like you haven't done:
#include <windows.h>
The OutputDebugString function is part of the Windows API.