# Why can't I call an assembly program from C++? I'm getting errors.

**URL:** <https://discourse.cmake.org/t/why-cant-i-call-an-assembly-program-from-c-im-getting-errors/709>\
**Category:** Code\
**Tags:** os:linux, lang:asm\
**Created:** [February 26, 2020, 8:25pm UTC](https://discourse.cmake.org/t/why-cant-i-call-an-assembly-program-from-c-im-getting-errors/709 "2020-02-26T20:25:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mike432](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mike432/32/395_2.png) [@Mike432](https://discourse.cmake.org/u/Mike432)\
**Post date:** [February 26, 2020, 8:25pm UTC](https://discourse.cmake.org/t/why-cant-i-call-an-assembly-program-from-c-im-getting-errors/709/1 "2020-02-26T20:25:01Z")

</div>

Can anyone give me a simple C++ program that calls a simple (but practical) assembly program (I don’t pass parameters)? What I’m currently trying to use is below.

I definitely have to have the options required to do this for my CMakeLists.txt file.

I’m trying to use code which I think is similar to the below snippet of code:

– file test.cpp —

> extern “C” int testing(void);  
> //…  
> testing();  
> //…

– file test.s —

> FUNC\_EXPORT(testing)
> 
> .text  
> .align 2  
> .long 0  
> .long 1  
> .globl testing  
> .type testing,%function  
> /\* … \*/

Thank you

P.S.: My compilation was good before I added the assembly call.

- List item

---

<div class="post-metadata">

**Author:** ![alex](https://discourse.cmake.org/user_avatar/discourse.cmake.org/alex/32/125_2.png) [@alex](https://discourse.cmake.org/u/alex)\
**Post date:** [February 26, 2020, 9:08pm UTC](https://discourse.cmake.org/t/why-cant-i-call-an-assembly-program-from-c-im-getting-errors/709/2 "2020-02-26T21:08:38Z")

</div>

I’m not sure which compiler/assembler you’re using, so I can’t comment on the assembly code itself. However, you will need to enable the ASM language in your project:

```auto
cmake_minimum_required(VERSION 3.11)
project(discourse709 LANGUAGES CXX ASM) # ASM must be last

add_executable(test test.cpp test.s)

```

If you’re using the GNU assembler (GAS), then the following files work for test.cpp and test.s:

> **test.cpp**
>
> ```auto
> #include <iostream>
> 
> extern "C" int my_asm_fun();
> 
> int main() {
> int value = my_asm_fun();
> std::cout << "value = " << value << "\n";
> return 0;
> }
> 
> ```

> **test.s**
>
> ```auto
> .globl my_asm_fun
> 
> .text
> my_asm_fun:
> mov $42, %rax
> ret
> 
> ```
