Search code examples
c++visual-studio-2022header-filesc++-modules

Supporting information from Programming Principles and Practice 3ed instructions unclear


In Stroustrup's PPP book, it says I should add #include "PPP.h" at the beginning of my programs. So I go to the book's website

Where he lists

I don't see an actual explanation on how to implement this.

And these are the errors I get from the Error List after attempting to put this together myself: Errors from the Error List

On the "How to use modules?" link, it only briefly mentions how to enable the use of modules. Which I've already done:

Microsoft C++

When using Visual Studio.

  • Module files are suffixed .ixx
  • Set the propeties of your project. Tab: Project -> Hello project.
    • General properties: Set "C++ language standard" to "latest"
    • C/C++ general: Scan for additional module dependencies: "yes"

I already made a test to check if I could use modules and it ran succesfully, using the following on another project:

// main.cpp
import test_module;

#include <iostream>

int main() {
    int result = add(5, 3);
    std::cout << "Result: " << result << std::endl;
    return 0;
}

and

// test_module.ixx
export module test_module; // use this syntax

export int add(int a, int b) {
    return a + b;
}

Console Output: Result: 8

The program I'm trying to build from the book and that is not working is structured as follows:

// ConsoleApplication1.cpp
#include "PPP.h"

int main()
{
    cout << "This is simplified compared to the actual program\n";
}
// PPP.h
import PPP;
using namespace PPP;
using namespace std;

// disgusting macro hack to guarantee range checking for []:
#define vector Checked_vector
#define string Checked_string
#define span Checked_span
// PPP.ixx
export module PPP;

export import std;

#define PPP_EXPORT export
#include "PPP_support.h"
using namespace PPP;

then I also have

// PPP_support.h
[...]

Which code I got from the referenced link PPP_support.h

My project settings look like this: Visual Code project Settings, C++ codign standard using "Latest C++" and this: Scan sources for Module Dependencies: Yes VC++ Directories:

And these are the errors I get from the Error List: Errors from the Error List

And from the Output window:

Build started at 7:27 PM...
1>------ Build started: Project: ConsoleApplication1, Configuration: Debug x64 ------
1>Scanning sources for module dependencies...
1>Compiling...
1>PPP.ixx
1>C:\dev\ppp3\ConsoleApplication1\PPP_support.h(86,25): error C2182: 'simple_error': this use of 'void' is not valid
1>(compiling source file 'PPP.ixx')
1>C:\dev\ppp3\ConsoleApplication1\PPP_support.h(86,38): error C2065: 'string': undeclared identifier
1>(compiling source file 'PPP.ixx')
1>C:\dev\ppp3\ConsoleApplication1\PPP_support.h(86,45): error C2146: syntax error: missing ')' before identifier 's'
1>(compiling source file 'PPP.ixx')
1>C:\dev\ppp3\ConsoleApplication1\PPP_support.h(87,2): error C2143: syntax error: missing ';' before '{'
1>(compiling source file 'PPP.ixx')
1>C:\dev\ppp3\ConsoleApplication1\PPP_support.h(87,2): error C2447: '{': missing function header (old-style formal list?)
1>(compiling source file 'PPP.ixx')
1>Done building project "ConsoleApplication1.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
========== Build completed at 7:27 PM and took 00.407 seconds ==========

I've also tried not using modules (although I think it should work because my small program testing module stest did work) and use "header "PPPheaders.h" for when you have to fall back to using header files". But I had no success trying that either, because I think the

#define PPP_EXPORT 
#include "PPP_support.h" 

takes me back to the starting point.

I really hope someone can help me out. I just bought the book this week with great excitement and I'm looking forward to learn C++.

I also read some other forums with questions on this same issue but haven't been able to find a concrete answer. The only solution I see for now is to not use this "support" module/header, and just import std; or #include <iostream>. But it does say the use of this is recommended for execises from deeper in the book.

Big thanks!


Solution

  • std is missing: //PPP_support.h

    PPP_EXPORT inline void simple_error(std::string s)  // write ``error: s'' and exit program (for non-exception terminating error handling)
    {
        std::cerr << "error: " << s << '\n';
        exit(1);
    }
    

    check the difference:

    enter image description here