0

I am learning embedded C. I am trying but giving error. I think this error due to mingw64. I am sharing full code

main.c

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>

#include "test/test.h"

int main()
{
  Test_t *deneme = test_init(5);
  int a = test_getCount(deneme);
  printf("one %d", a);
  test_updateCount(deneme, 10);
  int b = test_getCount(deneme);
  printf("two %d", b);
  return 0;
}

test.h

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>

typedef struct
{
  int count;
} Test_t;

Test_t *test_init(int number);
void test_updateCount(Test_t *tester, int number);
int test_getCount(Test_t *tester);

test.c

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>

#include "test.h"

Test_t *test_init(int number)
{
  Test_t *deneme = malloc(sizeof(Test_t));
  deneme->count = number;
}

void test_updateCount(Test_t *tester, int number)
{
  tester->count = number;
}

int test_getCount(Test_t *tester)
{
  return tester->count;
}

I am developing with VSCODE. test.c and test.h files is in directory ./test. build command is gcc .\main.c -o main.exe and build error

C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\user\AppData\Local\Temp\ccSxcYgu.o:main.c:(.text+0x13): undefined reference to `test_init'
C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\user\AppData\Local\Temp\ccSxcYgu.o:main.c:(.text+0x23): undefined reference to `test_getCount'
C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\user\AppData\Local\Temp\ccSxcYgu.o:main.c:(.text+0x4b): undefined reference to `test_updateCount'
C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\user\AppData\Local\Temp\ccSxcYgu.o:main.c:(.text+0x57): undefined reference to `test_getCount'
collect2.exe: error: ld returned 1 exit status
273K
  • 29,503
  • 10
  • 41
  • 64
Erdem Ün
  • 204
  • 1
  • 7
  • 1
    It's simple, all your unresolved errors reference functions in test.c. You need to add test.c to your build command `gcc main.c test/test.c -o main.exe`. You do this in VSCode by editing tasks.json. – john Aug 13 '23 at 20:56
  • 1
    @john thank you. it is work. how can I add every c file to this command ? I have job if I write every c files :D – Erdem Ün Aug 13 '23 at 20:59
  • 2
    VSCode doesn't work very well with multiple file builds. You can use wild cards, e.g. `*.c` for all C files in a directory. But if you have a lot of files it's probably better to use a build system like CMake, – john Aug 13 '23 at 21:02
  • 1
    I solved with cmake. thanks guys – Erdem Ün Aug 13 '23 at 21:55
  • 2
    Does this answer your question? [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) –  Aug 13 '23 at 22:16
  • C and C++ are very distinct programming languages. Don't tag the C code with C++. Linux and embedded tags are also irrelevant to you question. – 273K Aug 13 '23 at 22:18

2 Answers2

1

I installed cmake. and I created CMakeLists.txt project directory.

CMakeLists.txt

cmake_minimum_required(VERSION 3.10)

project(test)

# Kaynak dosyalarını belirtin
set(SOURCES
    main.c
    "test/test.c"
)

# Derleme hedefi oluşturma
add_executable(test ${SOURCES})

after I created build file on project directory and go to build file inside with command prompt. I run cmake .. command. and after go to project directory. I want to every save file and build and run. so I installed nodemon. last command run nodemon --watch "./" --ext "c" --exec "cmake --build ./build && .\build\Debug\test.exe"

Erdem Ün
  • 204
  • 1
  • 7
0

The compiler needs to know the implementation of test_init() , test_updateCount() and test_getCount() to link your program.

But first, rhe file test.h needs an include guard. You can achieve this by placing#pragma once as the first line of your header file.

The following command should build it: gcc -o main.exe main.c test/test.c

Additionally, I recommend using a Makefile or a modern programming IDE (as recommended by Lundin) to streamline your build process.

cforler
  • 179
  • 1
  • 7
  • Or just use a modern programming IDE and don't worry about make files and linker input. – Lundin Aug 14 '23 at 06:31
  • Maybe I am old-fashioned, but in my opinion, programmers should be able to compile and build a program without a modern programming IDE. – cforler Aug 14 '23 at 06:48
  • Yes that's old fashioned and we used to waste so much time on various command line junk back in the 1990s, time that could have been spent doing actual programming. Get a tool chain which comes with as little friction and setup as possible, so that you can focus on the actual job. – Lundin Aug 14 '23 at 06:50