I'm trying to learn some assembly code and I'm following a tutorial proposed by a book, I've got a C code defined as follows (Ch02_01.cpp) :
#include <iostream>
using namespace std;
extern "C" int IntegerAddSub_(int a, int b, int c, int d);
int main() {
int a, b, c, d, result;
a = 101; b = 34; c = -190; d = 25;
result = IntegerAddSub_(a, b, c, d);
cout << "result = " << result << n1;
return 0;
}
Knowing that The function IntegerAddSub_ is defined in an assembly file (asm extension) in the folder as follows (Ch02_01.asm) :
; extern "C" int IntegerAddSub_(int a, int b, int c, int d);
.code
IntegerAddSub_ proc
; Calculate a b c -d
mov eax, ecx ;eax = a
add eax, edx ;eax = a b
add eax, r8d ;eax = a b c
sub eax, r9d ;eax = a b c - d
ret ; return result to caller
IntegerAddSub_ endp
end
Error message :
Error message :
/home/.local/share/JetBrains/Toolbox/apps/CLion/ch-0/222.3345.126/bin/cmake/linux/bin/cmake --build /home/Assembly/cmake-build-debug --target Assembly -j 16
[1/1] Linking CXX executable Assembly
FAILED: Assembly
: && /usr/bin/c -g CMakeFiles/Assembly.dir/Ch02_01.cpp.o -o Assembly && :
/usr/bin/ld: CMakeFiles/Assembly.dir/Ch02_01.cpp.o: in function `main':
/home/Assembly/Ch02_01.cpp:25: undefined reference to `IntegerAddSub_'
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
I think the semantics of the error is simply that the C file did not recognize the function defined in the assembler file.
According to the exchanges in the comments, i'll add the following informations: I'am using CLION (From Jetbrains) and here is my CMakeLists.txt
cmake_minimum_required(VERSION 3.23)
project(Assembly)
set(CMAKE_CXX_STANDARD 14)
set ( SOURCES
Ch02_01.asm
)
add_executable(Assembly
Ch02_01.cpp)
And I don't know how to compile that using g , it never recognizes the function from the ASM file, how can I successfully compile that, please? I am in Ubuntu. Thanks
CodePudding user response:
You need to tell CMake that the project includes nasm code too - or masm if that's what it actually is.
Example:
cmake_minimum_required(VERSION 3.22)
project(Assembly CXX ASM_NASM) # or perhaps it should be ASM_MASM
set(CMAKE_CXX_STANDARD 14)
add_executable(Assembly Ch02_01.asm Ch02_01.cpp)
This makes it at least try to compile the asm
file, but for me it fails:
[ 33%] Building ASM_NASM object CMakeFiles/Assembly.dir/Ch02_01.asm.o
/home/ted/proj/stackoverflow/assembly/Ch02_01.asm:3: warning: label alone on a line without a colon might be in error [-w label-orphan]
/home/ted/proj/stackoverflow/assembly/Ch02_01.asm:4: error: parser: instruction expected
/home/ted/proj/stackoverflow/assembly/Ch02_01.asm:12: error: label `IntegerAddSub_' inconsistently redefined
/home/ted/proj/stackoverflow/assembly/Ch02_01.asm:4: info: label `IntegerAddSub_' originally defined here
/home/ted/proj/stackoverflow/assembly/Ch02_01.asm:12: error: parser: instruction expected
/home/ted/proj/stackoverflow/assembly/Ch02_01.asm:13: warning: label alone on a line without a colon might be in error [-w label-orphan]
make[2]: *** [CMakeFiles/Assembly.dir/build.make:76: CMakeFiles/Assembly.dir/Ch02_01.asm.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/Assembly.dir/all] Error 2
make: *** [Makefile:91: all] Error 2