Search code examples
ccmakeclion

Cannot run c file in clion


I configure CLion toolchain with MinGW and set environment variable in Windows 7, and I also can use GCC to complile and built and run hello.c in cmd console.

F:\c_cpp\
|-- cmake-build-debug\
|-- CMakeLists.txt
|-- hello.c
|-- library.c
`-- library.h

The following is my CMake file:

cmake_minimum_required(VERSION 3.8)
project(c_cpp)

set(CMAKE_C_STANDARD 99)

set(SOURCE_FILES library.c library.h hello.c)
add_library(c_cpp ${SOURCE_FILES})

When I click run button and choose "run c_cpp", the error happened.

Error running 'c_cpp': Cannot run program "F:\c_cpp\library.c" (in directory "F:\c_cpp"): CreateProcess error=193, %1 不是有效的 Win32 应用程序。

What should I do?


Solution

  • You are building a library, which you can't execute.

    If hello.c is an application using your library, change

    add_library(c_cpp ${SOURCE_FILES})
    

    to

    add_executable(hello ${SOURCE_FILES})
    

    After which you should be able to run hello.