I have to make a C multi module project in CLion. The project is called a2-dragos12312-a1, and in it I have a "c files" directory containing main.c and ui.c, and a "headers" directory containing ui.h. I made a CMakeLists.txt file in the project directory:
set(CMAKE_BUILD_TYPE Release)
cmake_minimum_required(VERSION 3.10)
project(a2-dragos12312-1)
# Include the headers directory
include_directories(headers, c files)
# Add the source files and create the executable
add_executable(a2-dragos12312-1 "c files/main.c" "c files/ui.c")
However, when I run it I get this error: undefined reference to `ui' collect2.exe: error: ld returned 1 exit status. I tried looking up hot to solve this nut didnt get any good way to fix it.
Tried making a CMakeLists.txt files containing what I said above, but it still doesnt run. If it helps my main.c is
#include "../headers/ui.h"
int main() {
ui();
return 0;
}
and my ui.h is
#pragma once
void ui();
In ui.c I do have a void ui() function which works.
I have to make a C multi module project in CLion. The project is called a2-dragos12312-a1, and in it I have a "c files" directory containing main.c and ui.c, and a "headers" directory containing ui.h. I made a CMakeLists.txt file in the project directory:
set(CMAKE_BUILD_TYPE Release)
cmake_minimum_required(VERSION 3.10)
project(a2-dragos12312-1)
# Include the headers directory
include_directories(headers, c files)
# Add the source files and create the executable
add_executable(a2-dragos12312-1 "c files/main.c" "c files/ui.c")
However, when I run it I get this error: undefined reference to `ui' collect2.exe: error: ld returned 1 exit status. I tried looking up hot to solve this nut didnt get any good way to fix it.
Tried making a CMakeLists.txt files containing what I said above, but it still doesnt run. If it helps my main.c is
#include "../headers/ui.h"
int main() {
ui();
return 0;
}
and my ui.h is
#pragma once
void ui();
In ui.c I do have a void ui() function which works.
Share Improve this question asked Mar 15 at 9:44 Dragos 123Dragos 123 211 silver badge2 bronze badges3 Answers
Reset to default 1You used a comma incorrectly in include_directories(headers, c files)
. Also, directory names containing spaces (like "c files"
) often cause problems.
Corrected CMakeLists.txt
:
cmake_minimum_required(VERSION 3.10)
project(a2-dragos12312-1 C)
set(CMAKE_BUILD_TYPE Release)
include_directories("headers" "c files")
add_executable(a2-dragos12312-1 "c files/main.c" "c files/ui.c")
Make sure your ui.c
file actually contains the definition matching exactly:
#include "..headers/ui.h"
void ui() {
// implementation
}
Consider renaming "c files"
to "src"
or "source"
to avoid issues caused by spaces.
I found a solution! It is from this thread: https://intellij-support.jetbrains/hc/en-us/community/posts/360010648239-CLion-stopped-recognising-project-as-a-CMake-project. All I had to do was close the project, delete the entire .idea folder and open it again.
Try changeing the order of files in add_executable
add_executable(a2-dragos12312-1 "c files/ui.c" "c files/main.c")