programing

C ++ 모듈을 준비하려면 C ++를 어떻게 작성해야합니까?

itsource 2021. 1. 16. 09:46
반응형

C ++ 모듈을 준비하려면 C ++를 어떻게 작성해야합니까?


C ++ 모듈을 지원하는 컴파일러는 이미 두 개 있습니다.

지금 새 프로젝트를 시작할 때 내 컴파일러에서 최종적으로 릴리스 될 때 모듈 기능을 채택 할 수 있으려면 무엇에주의해야합니까?

모듈을 사용하고이를 지원하지 않는 이전 컴파일러와의 호환성을 유지할 수 있습니까?


C ++ 모듈을 지원하는 컴파일러가 이미 두 개 있습니다.

clang : http://clang.llvm.org/docs/Modules.html MS VS 2015 : http://blogs.msdn.com/b/vcblog/archive/2015/12/03/c-modules-in-vs -2015-update-1.aspx

마이크로 소프트의 접근 방식이 가장 큰 관심을 받고있는 것으로 보입니다. 주로 마이크로 소프트가 현재 어떤 클랭 사람들보다 구현에 더 많은 리소스를 투입하고 있기 때문입니다. https://llvm.org/bugs/buglist.cgi?list_id=100798&query_format=advanced&component=Modules&product=clang을 참조 하십시오.내 말은, Modules for C ++에는 몇 가지 큰 버그가있는 반면, Modules for C 또는 특히 Objective C는 실제 코드에서 훨씬 더 유용 해 보입니다. Visual Studio의 가장 크고 중요한 고객 인 Microsoft는 수많은 내부 빌드 확장 성 문제를 해결하기 때문에 Modules를 열심히 추진하고 있으며 Microsoft의 내부 코드는 존재하는 곳 어디에서나 컴파일하기 가장 어려운 C ++ 중 일부이므로 컴파일러를 던질 수 없습니다. MSVC 이외의 것 (예 : clang 또는 GCC를 사용하여 40k 줄 함수를 컴파일하는 행운). 따라서 Google 등에서 사용하는 clang 빌드 트릭은 Microsoft에서 사용할 수 없으며 조만간 수정해야하는 절박한 요구가 있습니다.

실제로 대규모 실제 코드 기반에 적용 할 때 Microsoft 제안에 심각한 디자인 결함이 없다고 말하는 것은 아닙니다. 그러나 Gaby는 모듈에 대한 코드를 리팩토링해야하는 견해이며 동의하지 않는 한 그가 어디에서 왔는지 볼 수 있습니다.

지금 새 프로젝트를 시작할 때 내 컴파일러에서 최종적으로 릴리스 될 때 모듈 기능을 채택 할 수 있으려면 무엇에주의해야합니까?

Microsoft의 컴파일러가 현재 모듈을 구현할 것으로 예상되는 한, 라이브러리가 다음 모든 형태로 사용 가능한지 확인해야합니다.

  1. 동적 라이브러리
  2. 정적 라이브러리
  3. 헤더 전용 라이브러리

많은 사람들에게 매우 놀라운 점은 현재 구현 될 것으로 예상되는 C ++ 모듈이 이러한 차이를 유지한다는 것입니다. 따라서 이제 위의 세 가지 모두에 대한 C ++ 모듈 변형을 얻 습니다. 첫 번째는 사람들이 C ++ 모듈이 기대하는 것과 비슷합니다. 마지막으로 가장 유용한 미리 컴파일 된 헤더처럼 보입니다. 이러한 변형을 지원해야하는 이유는 거의 추가 작업없이 C ++ 모듈을 지원하기 위해 대부분의 동일한 전 처리기 기계를 재사용 할 수 있기 때문입니다.

이후 Visual Studio에서는 모듈 정의 파일 (.ifc 파일)을 리소스로 DLL에 연결할 수 있습니다. 이것은 마침내 MSVC에서 .lib 및 .dll 구별의 필요성을 제거 할 것입니다. 컴파일러에 단일 DLL을 제공하기 만하면 모듈 가져 오기에서 모두 "그냥 작동"하며 헤더 나 기타 필요한 것은 없습니다. 물론 이것은 COM과 약간 비슷하지만 COM의 이점은 거의 없습니다.

단일 코드베이스에서 모듈을 사용 하고이를 지원하지 않는 이전 컴파일러와의 호환성을 유지할 수 있습니까?

위에 삽입 된 굵은 텍스트를 의미한다고 가정하겠습니다.

대답은 일반적으로 더 많은 전 처리기 매크로 재미와 함께 예입니다. 전처리 기가 여전히 정상적으로 작동하기 때문에 헤더 내에서 #include <someheader>로 바뀔 수 있습니다 import someheader. 따라서 다음 줄과 같이 C ++ 모듈 지원을 사용하여 개별 라이브러리 헤더를 마크 업할 수 있습니다.

// someheader.hpp

#if MODULES_ENABLED
#  ifndef EXPORTING_MODULE
import someheader;  // Bring in the precompiled module from the database
// Do NOT set NEED_DEFINE so this include exits out doing nothing more
#  else
// We are at the generating the module stage, so mark up the namespace for export
#    define SOMEHEADER_DECL export
#    define NEED_DEFINE
#  endif
#else
// Modules are not turned on, so declare everything inline as per the old way
#  define SOMEHEADER_DECL
#  define NEED_DEFINE
#endif

#ifdef NEED_DEFINE
SOMEHEADER_DECL namespace someheader
{
  // usual classes and decls here
}
#endif

이제 main.cpp 또는 무엇이든 간단히 다음을 수행하십시오.

#include "someheader.hpp"

... 컴파일러에 / experimental : modules / DMODULES_ENABLED가있는 경우 애플리케이션은 라이브러리의 C ++ 모듈 에디션을 자동으로 사용합니다. 그렇지 않으면 우리가 항상 해왔 던 것처럼 인라인 포함됩니다.

I reckon these are the minimum possible set of changes to your source code to make your code Modules-ready now. You will note I have said nothing about build systems, this is because I am still debugging the cmake tooling I've written to get all this stuff to "just work" seamlessly and I expect to be debugging it for some months yet. Expect to see it maybe at a C++ conference next year or the year after :)


Is it possible to use modules and still maintain compatibility with older compilers that do not support it?

No, it is not possible. It might be possible using some #ifdef magic like this:

#ifdef CXX17_MODULES
    ...
#else
    #pragma once, #include "..." etc.
#endif

but this means you still need to provide .h support and thus lose all the benefits, plus your codebase looks quite ugly now.

If you do want to follow this approach, the easiest way to detect "CXX17_MODULES" which I just made up is to compile a small test program that uses modules with a build system of your choice, and define a global for everyone to see telling whether the compilation succeeded or not.

When starting a new project now, what should I pay attention to in order to be able to adopt the modules feature when it is eventually released in my compiler?

It depends. If your project is enterprise and gets you food on the plate, I'd wait a few years once it gets released in stables so that it becomes widely adapted. On the other hand, if your project can afford to be bleeding-edge, by all means, use modules.

Basically, it's the same story ast with Python3 and Python2, or less relevantly, PHP7 and PHP5. You need to find a balance between being a good up-to-date programmer and not annoying people on Debian ;-)

ReferenceURL : https://stackoverflow.com/questions/34652029/how-should-i-write-my-c-to-be-prepared-for-c-modules

반응형