Clion new method shows undefined reference to solution

Clion new method shows undefined reference to solution

Problem solving


"D:\cSoftware\CLion 2019.1.4\bin\cmake\win\bin\cmake.exe" --build D:\site\c\sm2\cmake-build-debug --target sm2 -- -j 4
-- Configuring done
-- Generating done
-- Build files have been written to: D:/site/c/sm2/cmake-build-debug
Scanning dependencies of target sm2
[ 50%] Linking C executable sm2.exe
d:/csoftware/mingw/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\sm2.dir/objects.a(main.c.obj): in function `main':
D:/site/c/sm2/main.c:6: undefined reference to `add'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [CMakeFiles\sm2.dir\build.make:85: sm2.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:72: CMakeFiles/sm2.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:84: CMakeFiles/sm2.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:130: sm2] Error 2

File addfunction. H

#include <stdio.h>
int add(int a,int b);

File addfunction. C

#include <stdio.h>
#include "addfunction.h"

int add(int a,int b){
    return a+b;


#include <stdio.h>
#include "addfunction.h"

int main() {
    int a = 1,b =2;
    int c = add(a,b);   //Here is a call to the add function in function.c
    return 0;

terms of settlement

Modify cmakelists.txt
and add_ Library and target_ link_ libraries

cmake_minimum_required(VERSION 3.14)
project(sm2 C)


add_library(sm2lib addfunction.h addfunction.c)
add_executable(sm2 main.c)
target_link_libraries(sm2 sm2lib)

Read More: