3

I have a project that written in C++17, let's name it A. It's using some C++17 specific features like std::optional, inline variables and etc...

My question is can I use that project as a third party library in a C++14 project (let's say project B) by compiling project A as static library and then use it in project B ? Even though headers of project A still has some functions and variables that declared using C++17 features ?

Anil Can
  • 55
  • 5
  • If you include those headers into project B then no. You will have to wrap them up in a c++14 compatible header with a c++ 17 cpp that calls the third party functions. – Fantastic Mr Fox Jul 28 '21 at 12:04
  • 1
    related/dupe: https://stackoverflow.com/questions/46746878/is-it-safe-to-link-c17-c14-and-c11-objects – NathanOliver Jul 28 '21 at 12:04
  • @FantasticMrFox So, it is okay only if I wrap project A's headers in c++14 but leave the cpp files as C++17 and link it as statci library to project B ? – Anil Can Jul 28 '21 at 12:28
  • 1
    Getting it compile and link is one problem. The other is ABI changes between C++ Standards and the compiler version(s) you are using. @NathanOliver linked the gcc information; you need to read the docs for the tool-chain version(s) you are using. As and example gcc has a workaround for the Standard change to `std::string` that disallowed copy-on-write. MSVC does not and breaks ABI compatability. – Richard Critten Jul 28 '21 at 12:45
  • Safely, no. Whether it works for a specific library or not is another issue. Compile all libraries using same compiler and compiler settings, if possible. Or, use a dynamic library (DLL) which might still have issues, but it is a safer bet. – Michael Chourdakis Jul 28 '21 at 12:49

0 Answers0