0

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;
}
Anonymoose
  • 38
  • 9
  • 3
    Unrelated to your problem with the compiler, but the code you show will most likely crash anyway, as you dereference an uninitialized pointer in `scan` function. – Some programmer dude Jun 05 '19 at 08:09
  • Thank you @Some programmer dude. That's true. In the longer version of the program I did initialize the pointers and added some data to the `source` array. I just shortened it up for the purpose of asking the question. – Anonymoose Jun 05 '19 at 08:13
  • There's no such thing as C/C++. C and C++ are different languages that share some features. – n. m. could be an AI Jun 05 '19 at 08:28
  • @n.m. Thank you. I just included both tags because I got different results. Under some conditions I got the program to compile and run when compiling as C code. But nothing compiled no matter what when compiling as C++. – Anonymoose Jun 05 '19 at 08:33
  • 1
    This is a bug in VS2019, the early releases suffer from the typical "write code fast and let the customer test it" agile bugs. For now you need to ignore that specific message. Just focus on the other errors that it reports. Once you fix those the MSB6006 error should disappear as well. – Hans Passant Jun 06 '19 at 08:42
  • @Some programmer dude. Thank you again. If I had understood the significance of what you "said" I would have saved myself some time. – Anonymoose Jun 07 '19 at 06:29
  • @Hans Passant. Thank you. I agree. Overall I think VS is a good product. This is the first time I've experienced a bug. I'll have to be more careful with my coding. – Anonymoose Jun 07 '19 at 06:30

1 Answers1

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.

Anonymoose
  • 38
  • 9