kevinbcarpenter/restful-with-billions

Project doesn't compile

Closed this issue · 6 comments

Under "Ubuntu 22.04 LTS", using the following versions:

  • cmake version 3.22.1
  • GNU Make 4.3
  • gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0

the project doesn't compile:

cd build
cmake ../
make -j`nproc`

Getting the following - showing an excerpt below:

...
In file included from /myproject/restful-with-billions/src/api/sale.cpp:7:
/myproject/restful-with-billions/src/data/store.h:21:52: error: ‘optional’ in namespace ‘std’ does not name a template type
   21 |   static auto get(const std::string &guid) -> std::optional<detail::cardInfo>;
      |                                                    ^~~~~~~~
/myproject/restful-with-billions/src/data/store.h:10:1: note: ‘std::optional’ is defined in header ‘<optional>’; did you forget to ‘#include <optional>’?
    9 | #include "detail/creditcard.h"
  +++ |+#include <optional>
   10 | 
/myproject/restful-with-billions/src/data/store.h:21:52: error: expected ‘;’ at end of member declaration
   21 |   static auto get(const std::string &guid) -> std::optional<detail::cardInfo>;
      |                                                    ^~~~~~~~
      |                                                            ;
/myproject/restful-with-billions/src/data/store.h:21:60: error: expected unqualified-id before ‘<’ token
   21 |   static auto get(const std::string &guid) -> std::optional<detail::cardInfo>;
...

So I just added #include <optional> to src/data/store.h - and everything compiles!

Using the given default config file restful.json, I also added a line to Project.cmake to create a folder <project-root>/bin/logs/, otherwise while running the test ./restful_functional_test the server ./restful will create tons of log messages:

Error opening: logs/restful.log errno: 2
Error opening: logs/restful.log errno: 2
Error opening: logs/restful.log errno: 2
Error opening: logs/restful.log errno: 2
...

You can get around this issues by compiling with C++20 (g++), if you have GCC-12 or newer

Change this line in the main cmake file: set(CMAKE_CXX_STANDARD 20)

Thanks @erasmussen1, I updated the CMake file.

Personally, I would prefer to include what is used in the code (among the list of standard includes like <map>, <mutex>, <string>, <iterator>, <iostream>, etc).

Anyway - now I installed GCC-12 and G++-12:

-- The C compiler identification is GNU 12.3.0
-- The CXX compiler identification is GNU 12.3.0

$> gcc --version
gcc (Ubuntu 12.3.0-1ubuntu1~22.04) 12.3.0
$> g++ --version
g++ (Ubuntu 12.3.0-1ubuntu1~22.04) 12.3.0

But the compile errors are the same (excerpt):

In file included from /localdisk/Cpp11pp14pp17pp20/restful-with-billions/src/api/sale.cpp:5:
/localdisk/Cpp11pp14pp17pp20/restful-with-billions/src/data/store.h:16:15: error: ‘optional’ in namespace ‘std’ does not name a te                                                 mplate type
   16 |   static std::optional<detail::cardInfo> get     ( const std::string& guid );
      |               ^~~~~~~~
/localdisk/Cpp11pp14pp17pp20/restful-with-billions/src/data/store.h:9:1: note: ‘std::optional’ is defined in header ‘<optional>’;                                                  did you forget to ‘#include <optional>’?
    8 | #include "detail/creditcard.h"
  +++ |+#include <optional>
    9 |

Just increasing to C++20 seems to NOT fix the compile errors; I checked the other header and source files - none of them includes <optional>.
(with older C++ versions you would need to add experimental namespace, wouldn't you?)

Personally, I would prefer to include what is used in the code (among the list of standard includes like , , , , , etc).

Anyway - now I installed GCC-12 and G++-12:

-- The C compiler identification is GNU 12.3.0
-- The CXX compiler identification is GNU 12.3.0

$> gcc --version
gcc (Ubuntu 12.3.0-1ubuntu1~22.04) 12.3.0
$> g++ --version
g++ (Ubuntu 12.3.0-1ubuntu1~22.04) 12.3.0

But the compile errors are the same (excerpt):

In file included from /localdisk/Cpp11pp14pp17pp20/restful-with-billions/src/api/sale.cpp:5:
/localdisk/Cpp11pp14pp17pp20/restful-with-billions/src/data/store.h:16:15: error: ‘optional’ in namespace ‘std’ does not name a te                                                 mplate type
   16 |   static std::optional<detail::cardInfo> get     ( const std::string& guid );
      |               ^~~~~~~~
/localdisk/Cpp11pp14pp17pp20/restful-with-billions/src/data/store.h:9:1: note: ‘std::optional’ is defined in header ‘<optional>’;                                                  did you forget to ‘#include <optional>’?
    8 | #include "detail/creditcard.h"
  +++ |+#include <optional>
    9 |

Just increasing to C++20 seems to NOT fix the compile errors; I checked the other header and source files - none of them includes . (with older C++ versions you would need to add experimental namespace, wouldn't you?)

Yes, if you do not want to change compiler, you can add the experimental flag to get std::optional

BTW: I switched my compiler to gcc-11 and compiled the project fine (with C++20 option), so there might be something else going on.

You might still using the default gcc and not gcc-20, and that might explain why you are getting the same error.
If this is the case, you can change the system default compiler to gcc-12 or you can modify the toolchain (toolchain-gcc.cmake ) to use gcc-12.

like this:
SET(CMAKE_SYSTEM_NAME Linux)

set(CMAKE_C_COMPILER gcc-12)
set(CMAKE_CXX_COMPILER g++-12)

The compilers are the correct ones (double-checked with update-alternatives), see also the cmake output mentioned above with the identified versions (The C compiler identification is GNU 12.3.0, The CXX compiler identification is GNU 12.3.0).

However, see the file "https://github.com/kevinbcarpenter/restful-with-billions/blob/main/CMakeLists.txt#L8", where set(CMAKE_CXX_STANDARD 17) still gets defined.

But @kevinbcarpenter just updated the file "https://github.com/kevinbcarpenter/restful-with-billions/blob/develop/Project.cmake#L3" and inserted set(CMAKE_CXX_STANDARD 20).

I can confirm, after replacing set(CMAKE_CXX_STANDARD 17) with set(CMAKE_CXX_STANDARD 20) I can successfully compile the project.

Feel free to consolidate both, CMakeLists.txt and Project.cmake.

So I just want to say thanks to you both for providing feedback. As you may note in my talk I point out there is a lot of this code that had not been updated. That said I did update the CMakeList.txt file instead of the Project.cmake acordingly.