Clion new method shows undefined reference to solution
Problem solving
problem
"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;
}
main.c
#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
printf("c=%d",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)
set(CMAKE_C_STANDARD 99)
add_library(sm2lib addfunction.h addfunction.c)
add_executable(sm2 main.c)
target_link_libraries(sm2 sm2lib)
Read More:
- [Solved] C++ error: undefined reference to `xxx‘
- [Solved] cortex_M3 freeRTOS Error: undefined reference to `_sbrk‘
- error: undefined reference to `calculate()` [How to Solve]
- [Solved] Qt Error: undefined reference to xxxxx
- [Solved] PCL Compile Error: undefined reference to `pcl::PCLBase<pcl::PointXYZRGBA>:: XXX
- [Solved] Compile Error: undefined reference to `google::FlagRegisterer::FlagRegisterer
- [Solved] QT error: error: undefined reference to ` VTable`
- C++Qt development – Qtsocket programming error: undefined reference to `_imp__WSAStartup
- GCC Error:(.text+0x24): undefined reference to `main‘collect2: error: ld returned 1 exit status [Solved]
- [Solved] Compile the program error: undefined reference to `cv::dnn::dnn4_v20210301::Net::~Net()‘
- Call to undefined method Illuminate\Foundation\Application::bindShared()
- [Solved] VScode Error: undefined reference to ‘WinMain’ collect2.exe: error: ld returned 1 exit status
- [Solved] QT Error: error: undefined reference to `GameModel::~GameModel()’
- JZVideo Error: Attempt to invoke virtual method ‘android.view.Window android.app.Activity.getWindow()’ on a null object reference
- [Solved] Searching for inspections failed: undefined method `map‘ for nil:NilClass“
- [Solved] cocopod Error: failed: undefined method `map‘ for nil:NilClass
- Wechat program typeerror: a solution to cannot read property ‘SetData’ of undefined
- Springboot running shows application run failed [How to Solve]
- Angular_ Error: Cannot assign to a reference or variable!
- [Solved] iperf Analyze Error: unable to create a new stream: Invalid argument