1

running CLion on Linux, I've been trying to get .asm files to work. This is how my test project looks like (it worked 100% on Windows/Mac, only had to add .intel_syntax in the .asm file).

Installed the NASM Assembly Support plugin.

main.c:

#include <stdio.h>

extern long tes(long);

int main() {
    printf("%ld", tes(5));
    printf("Hello, World!\n");
    return 0;
}

tes.asm:

.text:

.global tes

tes:
    mov rax, edi
    ret

CMakeLists.txt:

cmake_minimum_required(VERSION 3.15)
project(Testt C)
enable_language(C ASM)

set(CMAKE_C_STANDARD 99)

set ( SOURCES 
        main.c 
        tes.asm)

add_executable(program  ${SOURCES} )

How do I configure CLion so as to use tes as a method in C? I'm expecting tes(x) = x, but somehow, my project doesn't compile:

 ====================[ Build | program | Debug ]=================================
/snap/clion/99/bin/cmake/linux/bin/cmake --build /home/user/CLionProjects/Testt/cmake-build-debug --target program -- -j 4
make: *** No rule to make target 'program'.  Stop.

Any help is appreciated!

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
melian
  • 11
  • 2
  • I know you didn't ask this, and didn't get that far yet, but for future reference: your code won't assemble with `nasm` for multiple reasons. – Jester Dec 30 '19 at 00:28
  • Thank you for letting me know, I'm puzzled as it worked perfectly on Windows & Mac today. Why isn't it working? And how do you recommand to integrate .asm files instead? – melian Dec 30 '19 at 00:48
  • 2
    I can't help you with the cmake stuff, unfortunately. But `.global` is not nasm syntax (delete the leading dot) and `mov rax, edi` is invalid due to different sized registers. Also `.text:` is a label, you probably meant `section .text` (but that is the default anyway). – Jester Dec 30 '19 at 00:57
  • I tried this, still didn't work. It runs on Mac, though. – melian Dec 30 '19 at 12:37
  • You don't need to enable C as this is enabled anyway. – Devolus Jan 01 '20 at 13:06
  • Any chance this answer is of any help? https://stackoverflow.com/a/49134624/3857942 – Michael Petch Jan 01 '20 at 13:41

1 Answers1

1

You need to use

enable_language(ASM_NASM)

Not sure if the CMake rules will recognize the .asm extension, in that case you might have to add

set_source_files_properties(${ASM_SOURCES} PROPERTIES LANGUAGE ASM_NASM)

Just created these files and they work using MKSYS MSYS with gcc 9 and nasm 2.14:

main.c

#include <stdio.h>

extern long test(long);

int main()
{
    printf("%ld", test(5));
    printf("Hello, World!\n");
    return 0;
}

test.asm

section text

global test

test:
    mov rax, 0
    ret

CMakeLists.txt

cmake_minimum_required(VERSION 3.15)
project(Test)
enable_language(ASM_NASM)

set(CMAKE_C_STANDARD 99)

set(ASM_SOURCES
     test.asm
)

set (SOURCES
     main.c 
     ${ASM_SOURCES}
)

set_source_files_properties(${ASM_SOURCES} PROPERTIES LANGUAGE ASM_NASM)

add_executable(program  ${SOURCES})
Devolus
  • 21,661
  • 13
  • 66
  • 113
  • Thanks! The first command didn't change anything, the second command throws an error message asking for a CMake NASM compiler. That might be the problem. It's asking for those files: CMakeDetermineNASMCompiler.cmake CMakeNASMCompiler.cmake and the variables CMAKE_NASM_COMPILER_ENV_VAR CMAKE_NASM_COMPILER Where is this NASM compiler? – melian Jan 01 '20 at 01:23
  • You can look into your cmake installation folder. There should be a modules directory which contains all the compilers known to CMake. Check if there is some file with NASM in the name. i.E. I have CMake 3.15 installed and there is "CMakeASM_NASMInformation.cmake" which means the language name is "ASM_NASM" in this case. I think that files should be installed along with CMake. Did you use "ASM_NASM" with the underscore? If cmake complains about CMakeDetermineNASM... it means that you used "NASM" as the language, as cmake was looking for that. – Devolus Jan 01 '20 at 13:01