Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
C++
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Compatibility== To give compiler vendors greater freedom, the C++ standards committee decided not to dictate the implementation of [[name mangling]], [[exception handling]], and other implementation-specific features. The downside of this decision is that [[object code]] produced by different [[compiler]]s is expected to be incompatible. There are, however, attempts to standardize compilers for particular machines or [[operating system]]s. For example, the Itanium C++ ABI is processor-independent (despite its name) and is implemented by GCC and Clang.<ref>{{cite web |url=https://mentorembedded.github.io/cxx-abi/ |title=C++ ABI Summary |date=20 March 2001 |access-date=30 May 2006 |archive-date=10 July 2018 |archive-url=https://web.archive.org/web/20180710195559/https://mentorembedded.github.io/cxx-abi/ |url-status=live }}</ref> ===With C=== {{Main|Compatibility of C and C++}} C++ is often considered to be a superset of [[C (programming language)|C]] but this is not strictly true.<ref name="superset">{{cite web |url=http://www.stroustrup.com/bs_faq.html#C-is-subset |title=Bjarne Stroustrup's FAQ – Is C a subset of C++? |access-date=5 May 2014 |archive-date=6 February 2016 |archive-url=https://web.archive.org/web/20160206214150/http://www.stroustrup.com/bs_faq.html#C-is-subset |url-status=live }}</ref> Most C code can easily be made to compile correctly in C++ but there are a few differences that cause some valid C code to be invalid or behave differently in C++. For example, C allows implicit conversion from <syntaxhighlight lang="C++" inline>void*</syntaxhighlight> to other pointer types but C++ does not (for type safety reasons). Also, C++ defines many new keywords, such as <syntaxhighlight lang="C++" inline>new</syntaxhighlight> and <syntaxhighlight lang="C++" inline>class</syntaxhighlight>, which may be used as identifiers (for example, variable names) in a C program. Some incompatibilities have been removed by the 1999 revision of the C standard ([[C99]]), which now supports C++ features such as line comments (<syntaxhighlight lang="C++" inline>//</syntaxhighlight>) and declarations mixed with code. On the other hand, C99 introduced a number of new features that C++ did not support that were incompatible or redundant in C++, such as [[variable-length array]]s, native complex-number types (however, the <syntaxhighlight lang="C++" inline>std::complex</syntaxhighlight> class in the C++ standard library provides similar functionality, although not code-compatible), designated initializers, [[C syntax#Compound literals|compound literals]], and the <syntaxhighlight lang="C++" inline>restrict</syntaxhighlight> keyword.<ref>{{cite web |url=http://home.datacomm.ch/t_wolf/tw/c/c9x_changes.html |title=C9X – The New C Standard |access-date=27 December 2008 |archive-date=21 June 2018 |archive-url=https://web.archive.org/web/20180621084656/http://home.datacomm.ch/t_wolf/tw/c/c9x_changes.html |url-status=live }}</ref> Some of the C99-introduced features were included in the subsequent version of the C++ standard, [[C++11#Improved C compatibility|C++11]] (out of those which were not redundant).<ref>{{cite web |title=C++0x Support in GCC |url=https://gcc.gnu.org/projects/cxx0x.html |access-date=12 October 2010 |archive-date=21 July 2010 |archive-url=https://web.archive.org/web/20100721215324/http://gcc.gnu.org/projects/cxx0x.html |url-status=live }}</ref><ref>{{cite web |title=C++0x Core Language Features In VC10: The Table |url=https://blogs.msdn.com/b/vcblog/archive/2010/04/06/c-0x-core-language-features-in-vc10-the-table.aspx |access-date=12 October 2010 |archive-date=21 August 2010 |archive-url=https://web.archive.org/web/20100821114635/http://blogs.msdn.com/b/vcblog/archive/2010/04/06/c-0x-core-language-features-in-vc10-the-table.aspx |url-status=live }}</ref><ref>{{cite web |url=https://clang.llvm.org/cxx_status.html |title=Clang - C++98, C++11, and C++14 Status |publisher=Clang.llvm.org |date=12 May 2013 |access-date=10 June 2013 |archive-date=4 July 2013 |archive-url=https://web.archive.org/web/20130704124639/http://clang.llvm.org/cxx_status.html |url-status=live }}</ref> However, the C++11 standard introduces new incompatibilities, such as disallowing assignment of a string literal to a character pointer, which remains valid C. To intermix C and C++ code, any function declaration or definition that is to be called from/used both in C and C++ must be declared with C linkage by placing it within an <syntaxhighlight style=white-space:nowrap lang="C++" inline>extern "C" {/*...*/}</syntaxhighlight> block. Such a function may not rely on features depending on [[name mangling]] (i.e., function overloading). ===Inline assembly=== Programs developed in C or C++ often utilize inline assembly to take advantage of its low-level functionalities, greater speed, and enhanced control compared to high-level programming languages<ref name="Bokil2021">Bokil, Milind A. (2021). "[https://www.researchgate.net/publication/354744729_Writing_Assembly_Routines_within_CC_and_Java_Programs Writing Assembly Routines within C/C++ and Java Programs]". ResearchGate. Retrieved 1 April 2025.</ref><ref name="Vilhena2024">{{cite journal | url=https://doi.org/10.1145/3689749 | doi=10.1145/3689749 | title=Extending the C/C++ Memory Model with Inline Assembly | date=2024 | last1=De Vilhena | first1=Paulo Emílio | last2=Lahav | first2=Ori | last3=Vafeiadis | first3=Viktor | last4=Raad | first4=Azalea | journal=Proceedings of the ACM on Programming Languages | volume=8 | pages=1081–1107 | arxiv=2408.17208 }}</ref> when optimizing for performance is essential. C++ provides support for embedding [[Assembly language|assembly]] language using asm declarations,<ref name="cppreferenceAsm">cppreference.com contributors. "[https://en.cppreference.com/w/cpp/language/asm asm declaration]". ''cppreference.com''. Retrieved 1 April 2025.</ref> but the compatibility of [[inline assembly]] varies significantly between [[compilers]] and architectures. Unlike high-level language features such as [[Python (programming language)|Python]] or [[Java (programming language) |Java]], assembly code is highly dependent on the underlying processor and compiler implementation. ====Variations across compilers==== Different C++ compilers implement inline assembly in distinct ways. * GCC ([[GNU Compiler Collection]]) and [[Clang]]:<ref>{{Cite web |title=Extended Asm (Using the GNU Compiler Collection) |url=https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html |website=GCC Online Documentation |publisher=GNU Project |access-date=1 April 2025}}</ref> Use the GCC extended inline assembly syntax. Using <syntaxhighlight lang="C++" inline>__asm__</syntaxhighlight> keyword instead of <syntaxhighlight lang="C++" inline>asm</syntaxhighlight> when writing code that can be compiled with <syntaxhighlight lang="C++" inline>-ansi</syntaxhighlight> and <syntaxhighlight lang="C++" inline>-std</syntaxhighlight> options, which allows specifying input/output operands and clobbered registers. This approach is widely adopted, including by Intel<ref name="IntelInlineAssembly">Intel Corporation. "[https://www.intel.com/content/www/us/en/docs/cpp-compiler/developer-guide-reference/2021-9/inline-assembly.html Inline Assembly]". ''Intel® C++ Compiler Classic Developer Guide and Reference'', Version 2021.9. Retrieved 1 April 2025.</ref> and IBM<ref name="IBMInlineAssembly">IBM. "[https://www.ibm.com/docs/en/xl-c-aix/13.1.3?topic=statements-inline-assembly-extension Inline assembly statements (IBM extension)]". ''IBM Documentation''. Retrieved 1 April 2025.</ref> compilers. * MSVC ([[Microsoft Visual C++]]): The inline assembler is built into the compiler. Previously supported inline assembly via the <syntaxhighlight lang="C++" inline>__asm</syntaxhighlight> keyword, but this support has been removed in 64-bit mode, requiring separate .asm modules instead.<ref>{{Cite web |title=Inline Assembler Overview |url=https://learn.microsoft.com/en-us/cpp/assembler/inline/inline-assembler-overview?view=msvc-170 |website=Microsoft Learn |publisher=Microsoft |access-date=1 April 2025}}</ref> * TI ARM Clang and Embedded Compilers:<ref>{{Cite web |title=Interfacing C and C++ With Assembly Language |url=https://software-dl.ti.com/codegen/docs/tiarmclang/compiler_tools_user_guide/compiler_manual/runtime_environment/interfacing-c-and-c-with-assembly-language-stdz0544217.html#interfacing-c-and-c-with-assembly-language |website=Texas Instruments |publisher=Texas Instruments Incorporated |date=23 February 2025 |access-date=1 April 2025}}</ref> Some embedded system compilers, like Texas Instruments' TI Arm Clang, allow inline assembly but impose stricter rules to avoid conflicts with register conventions and calling conventions. ====Interoperability between C++ and Assembly==== C++ provides two primary methods of integrating ASM code. 1. Standalone assembly files – Assembly code is written separately and linked with C++ code.<ref>{{cite web |url=https://wiki.osdev.org/C%2B%2B_to_ASM_linkage_in_GCC |title=C++ to ASM linkage in GCC |website=OSDev Wiki |access-date=1 April 2025}}</ref> 2. [[Inline assembly]] – Assembly code is embedded within C++ code using compiler-specific extensions. ;Example Code for ASM Compatibility * When calling an assembly function from C++, use <syntaxhighlight lang="C++" inline>extern "C"</syntaxhighlight> to prevent C++ name mangling. <syntaxhighlight lang="cpp" line="1"> //main.cpp import std; extern "C" int add_asm(int, int); // Declare the assembly function int main() { int result = add_asm(5, 7); std::println("Result from ASM: {}", result); return 0; } </syntaxhighlight> <syntaxhighlight lang="cpp" line="1"> #asm code using RISC-V architecture .section .text .global add_asm add_asm: add a0, a0, a1 # Add first argument (a0) and second argument (a1), store in a0 ret # Return (a0 holds return value) </syntaxhighlight> *Global variables in assembly must be declared as <syntaxhighlight lang="C++" inline>extern</syntaxhighlight> in C++ and marked <code>.global</code> in assembly. <syntaxhighlight lang="cpp" line="1"> // main.cpp import std; extern "C" int global_var; // Declare global variable from assembly int main() { std::println("Global variable from ASM: {}", global_var); return 0; } </syntaxhighlight> <syntaxhighlight lang="cpp" line="1"> #asm using RISC-V architecture .section .data .global global_var .align 4 global_var: .word 42 # Define integer value </syntaxhighlight> * Inline assembly allows embedding ASM directly in C++ using the <syntaxhighlight lang="C++" inline>asm</syntaxhighlight> keyword. <syntaxhighlight lang="cpp" line="1"> //main.cpp (using GCC/CLANG compiler) import std; int main() { int x = 10, y = 20, sum; asm volatile ( "add %0, %1, %2" : "=r" (sum) // Output operand (stored in a register) : "r" (x), "r" (y) // Input operands (stored in registers) ); std::println("Sum using inline ASM: {}", sum); return 0; } </syntaxhighlight>
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
C++
(section)
Add topic