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

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

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:

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
#include <iostream>

extern "C" int my_asm_fun();

int main() {
    int value = my_asm_fun();
    std::cout << "value = " << value << "\n";
    return 0;
}
test.s
    .globl my_asm_fun

    .text
my_asm_fun:
    mov $42, %rax
    ret