How do I pass arugments to the compile in Clion?

Answered

I have looked over the internet, forums and even asked chatGPT and nothing seems to work? How do I add those flags

-fsanitize=address,undefined -Wall -Werror

I originally added to the Cmakelist.text

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address, undefined")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror")

However, it throws an error cause undefined is there? removing undefined fixes the error but when I try to get an index out of bounds error nothing happens ;'C
Also please let me know if you would recommend other flags lets the compiler tell you something is wrong.

0
15 comments

Db988

1) The second point of the instruction states "Set Architecture to x86_amd64". According to the provided screenshot, you haven't done it.
2) According to

"C:\Program Files\JetBrains\CLion 2022.3.1\bin\cmake\win\x64\bin\cmake.exe" -DCMAKE_BUILD_TYPE=Debug 

you still use the Debug profile, while you need to use the Release profile.

Please select the Release profile in the Run/Debug configuration switcher:

3) Remove the following line from the CMakeLists.txt

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address,undefined")

4) Add the following line to the CMakeLists.txt

target_compile_options(untitled PRIVATE -fsanitize=address)
1

You need to remove the space between `address` and `undefined`.

0

Thank you Andy for getting back to me, I truly appreciate it.

I have followed your advice however, I get a different kind op error when I try to run my program

#include <stdio.h>

int main(void) {
int array1[5] = {1, 2};

printf("%i",array1[1]);
printf("Hello, World!\n");
return 0;
}

The error I get. 

"C:\Program Files\JetBrains\CLion 2022.3.1\bin\cmake\win\x64\bin\cmake.exe" --build C:\Users\danie\CLionProjects\untitled\cmake-build-release-visual-studio --target untitled -j 9
[1/1] Linking C executable untitled.exe
FAILED: untitled.exe 
cmd.exe /C "cd . && "C:\Program Files\JetBrains\CLion 2022.3.1\bin\cmake\win\x64\bin\cmake.exe" -E vs_link_exe --intdir=CMakeFiles\untitled.dir --rc=C:\PROGRA~2\WI3CF2~1\10\bin\100220~1.0\x86\rc.exe --mt=C:\PROGRA~2\WI3CF2~1\10\bin\100220~1.0\x86\mt.exe --manifests  -- C:\PROGRA~1\LLVM\bin\lld-link.exe /nologo CMakeFiles\untitled.dir\main.c.obj  /out:untitled.exe /implib:untitled.lib /pdb:untitled.pdb /version:0.0 /machine:X86 /INCREMENTAL:NO /subsystem:console  kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cd ."
LINK: command "C:\PROGRA~1\LLVM\bin\lld-link.exe /nologo CMakeFiles\untitled.dir\main.c.obj /out:untitled.exe /implib:untitled.lib /pdb:untitled.pdb /version:0.0 /machine:X86 /INCREMENTAL:NO /subsystem:console kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:untitled.exe.manifest" failed (exit code 1) with the following output:
lld-link: error: undefined symbol: ___asan_option_detect_stack_use_after_return
>>> referenced by CMakeFiles\untitled.dir\main.c.obj:(_printf)

lld-link: error: undefined symbol: ___asan_stack_malloc_0
>>> referenced by CMakeFiles\untitled.dir\main.c.obj:(_printf)

lld-link: error: undefined symbol: ___asan_report_load4
>>> referenced by CMakeFiles\untitled.dir\main.c.obj:(_printf)

lld-link: error: undefined symbol: ___ubsan_handle_type_mismatch_v1
>>> referenced by CMakeFiles\untitled.dir\main.c.obj:(_printf)

lld-link: error: undefined symbol: ___asan_report_load8
>>> referenced by CMakeFiles\untitled.dir\main.c.obj:(_printf)

lld-link: error: undefined symbol: ___asan_init
>>> referenced by CMakeFiles\untitled.dir\main.c.obj:(_asan.module_ctor)

lld-link: error: undefined symbol: ___asan_version_mismatch_check_v8
>>> referenced by CMakeFiles\untitled.dir\main.c.obj:(_asan.module_ctor)
ninja: build stopped: subcommand failed.

 

Also, I wanted to double check that I must be using Clang Cli instead of GCC, as sanitization isn't available on GCC on windows, is that. correct?

 

Thank you!

0

OK so you need to link against the `asan` library in order to find those symbols. See target_link_libraries

0

Db988 please take a look at our web help article - https://www.jetbrains.com/help/clion/google-sanitizers.html. On Windows it's possible to use Google sanitizers only in the two following cases:

0

Okay, thank you for clarifying Anna, I am using MSVC+Clang-cl Toolchain, however I have no idea what the error above means, would you mind helping me?

0

Anna Falevskaya I have followed it, which meant I add these new lines into the Cmakelists.txt file 

target_link_directories(untitled PRIVATE "C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/Llvm/x64/lib/clang/15.0.1/lib/windows")
target_link_libraries(untitled PRIVATE clang_rt.asan_dynamic-x86_64 clang_rt.asan_dynamic_runtime_thunk-x86_64)
target_link_options(untitled PRIVATE /wholearchive:clang_rt.asan_dynamic_runtime_thunk-x86_64.lib)

The flags work fine, they give me errors when I am not doing what I am supposed to do. However building and running the projects gives me these errors

FAILED: untitled.exe 
cmd.exe /C "cd . && "C:\Program Files\JetBrains\CLion 2022.3.1\bin\cmake\win\x64\bin\cmake.exe" -E vs_link_exe --intdir=CMakeFiles\untitled.dir --rc=C:\PROGRA~2\WI3CF2~1\10\bin\100220~1.0\x86\rc.exe --mt=C:\PROGRA~2\WI3CF2~1\10\bin\100220~1.0\x86\mt.exe --manifests  -- C:\PROGRA~1\LLVM\bin\lld-link.exe /nologo CMakeFiles\untitled.dir\main.c.obj  /out:untitled.exe /implib:untitled.lib /pdb:untitled.pdb /version:0.0 /machine:X86 /INCREMENTAL:NO /subsystem:console  /wholearchive:clang_rt.asan_dynamic_runtime_thunk-x86_64.lib -LIBPATH:C:\PROGRA~1\MIB055~1\2022\COMMUN~1\VC\Tools\Llvm\x64\lib\clang\150~1.1\lib\windows clang_rt.asan_dynamic-x86_64.lib  clang_rt.asan_dynamic_runtime_thunk-x86_64.lib  kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cd ."
LINK: command "C:\PROGRA~1\LLVM\bin\lld-link.exe /nologo CMakeFiles\untitled.dir\main.c.obj /out:untitled.exe /implib:untitled.lib /pdb:untitled.pdb /version:0.0 /machine:X86 /INCREMENTAL:NO /subsystem:console /wholearchive:clang_rt.asan_dynamic_runtime_thunk-x86_64.lib -LIBPATH:C:\PROGRA~1\MIB055~1\2022\COMMUN~1\VC\Tools\Llvm\x64\lib\clang\150~1.1\lib\windows clang_rt.asan_dynamic-x86_64.lib clang_rt.asan_dynamic_runtime_thunk-x86_64.lib kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:untitled.exe.manifest" failed (exit code 1) with the following output:
lld-link: error: clang_rt.asan_dynamic_runtime_thunk-x86_64.lib(asan_globals_win.cpp.obj): machine type x64 conflicts with x86
lld-link: error: clang_rt.asan_dynamic_runtime_thunk-x86_64.lib(asan_win_dynamic_runtime_thunk.cpp.obj): machine type x64 conflicts with x86
lld-link: error: clang_rt.asan_dynamic_runtime_thunk-x86_64.lib(ubsan_win_dynamic_runtime_thunk.cpp.obj): machine type x64 conflicts with x86
lld-link: error: clang_rt.asan_dynamic_runtime_thunk-x86_64.lib(sanitizer_coverage_win_dynamic_runtime_thunk.cpp.obj): machine type x64 conflicts with x86
lld-link: error: clang_rt.asan_dynamic_runtime_thunk-x86_64.lib(sanitizer_coverage_win_sections.cpp.obj): machine type x64 conflicts with x86
lld-link: error: clang_rt.asan_dynamic_runtime_thunk-x86_64.lib(sanitizer_win_dynamic_runtime_thunk.cpp.obj): machine type x64 conflicts with x86
ninja: build stopped: subcommand failed.

I don't know how to fix it? Why is this more difficult than it should be: C, is there and IDE has a simple button I can click 🥺😭

 

 

0

Db988

  1. Please show a screenshot of your MSVC toolchain (`File | Settings | Build, Execution, Deployment | Toolchains`)
  2. Please show a screenshot of `File | Settings | Build, Execution, Deployment | CMake`.
  3. Please do `Tools | CMake | Reset Cache and Reload Project` and paste here the entire output from the CMake tool window.
  4. Paste here your entire CMakeLists.txt.

 

0
  1.  

  2. "C:\Program Files\JetBrains\CLion 2022.3.1\bin\cmake\win\x64\bin\cmake.exe" -DCMAKE_BUILD_TYPE=Debug "-DCMAKE_MAKE_PROGRAM=C:/Program Files/JetBrains/CLion 2022.3.1/bin/ninja/win/x64/ninja.exe" "-DCMAKE_C_COMPILER=C:/Program Files/LLVM/bin/clang-cl.exe" "-DCMAKE_CXX_COMPILER=C:/Program Files/LLVM/bin/clang-cl.exe" -DCMAKE_C_FLAGS=-m32 -DCMAKE_CXX_FLAGS=-m32 -G Ninja -S C:\Users\danie\CLionProjects\untitled -B C:\Users\danie\CLionProjects\untitled\cmake-build-debug-1
    -- The C compiler identification is Clang 15.0.7 with MSVC-like command-line
    -- Detecting C compiler ABI info
    -- Detecting C compiler ABI info - done
    -- Check for working C compiler: C:/Program Files/LLVM/bin/clang-cl.exe - skipped
    -- Detecting C compile features
    -- Detecting C compile features - done
    -- Configuring done
    -- Generating done
    CMake Warning:
      Manually-specified variables were not used by the project:

        CMAKE_CXX_COMPILER
        CMAKE_CXX_FLAGS


    -- Build files have been written to: C:/Users/danie/CLionProjects/untitled/cmake-build-debug-1

    [Finished]

     

  3. cmake_minimum_required(VERSION 3.24)
    project(untitled C)

    set(CMAKE_C_STANDARD 23)

    add_executable(untitled main.c)
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address,undefined")
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror")
    target_link_directories(untitled PRIVATE "C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/Llvm/x64/lib/clang/15.0.1/lib/windows")
    target_link_libraries(untitled PRIVATE clang_rt.asan_dynamic-x86_64 clang_rt.asan_dynamic_runtime_thunk-x86_64)
    target_link_options(untitled PRIVATE /wholearchive:clang_rt.asan_dynamic_runtime_thunk-x86_64.lib)
0

Anna Falevskaya

Thank you so much it works now but there is a problem, nothing is printing at all.

the console became read only as well?

0

As you can see, there is the error - "Process finished with exit code -1073741515 (0xC0000135)". The 0xc0000135 error indicates that the application failed to find a DLL file required for the program to operate correctly, so the program could not start.

There are several ways to solve the issue:

  1. Go to Run | Edit Configurations ..., select your run/debug configuration and try adding C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/Llvm/x64/lib/clang/15.0.1/lib/windows to the PATH environment variable in the Environment Variables field (like Path=xxx;$Path$, where xxx is the path to that directory).
  2. Copy all the files from C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/Llvm/x64/lib/clang/15.0.1/lib/windows into the cmake-build-release folder.
  3. Add set(CMAKE_EXE_LINKER_FLAGS "-static") to the CMakeLists.txt.
0

Anna Falevskaya, I have followed the steps you mentioned above and, now I get a different error code.

Process finished with exit code -1073741502 (0xC0000142)

I have applied all methods and the only one that seems to work (changing the error code) is step 2. Attached below are screenshots of me applying each method.

I have also searched the error code online and found that it could be cause by my office 360 not update, or corrupted windows files both of which I have checked and restarted my computer after. I just wanted to say I appreciate you helping me, and honestly putting the time and effort out of your day just to help me 😊

 

0

In the "Environment Variables" window the second column is called "Value". It means you need to specify only the value of the variable in this column, without Path= :

The format Path=xxx;$Path$ should be used only if you specify it right in the "Environment variables" field:

0

Anna Falevskaya Thank you for replying,

I did just that, but I still get the same error code. I also updated my graphics driver, but it still wont work. I retried doing the same steps on a second computer with a clean install of windows but it also wont work.

Here is the error on my second computer. I know its a path issue, I even tried adding the path of that file to system variables which then became grayed out??

C:\Program Files\LLVM\bin\clang-cl.exe" -o main C:\Users\danie\CLionProjects\untitled\main.c LINK : fatal error LNK1104: cannot open file 'libcmt.lib' clang-cl: error: linker command failed with exit code 1104 (use -v to see invocation)

0

Please sign in to leave a comment.