Why Ceedling can't link the functions from other source files together? #1013
Replies: 4 comments 8 replies
-
|
I've noticed something that might help but i can't get why. The error is produced when having multiple test files that include any .h files. |
Beta Was this translation helpful? Give feedback.
-
|
Why is this a discussion and not an issue? What do you want to discuss about? That ChatGPT is not giving you complete project.yml configuration? |
Beta Was this translation helpful? Give feedback.
-
|
I just created empty |
Beta Was this translation helpful? Give feedback.
-
|
Hi @AhmedMagdy111 -- Ceedling lets you specify which files are needed for a given test in two ways. You can use either or both in each test file. Both are specified in the test file itself!
The configuration in the yaml file tells ceedling where to find ALL the source files and includes. Each test, however, becomes its own executable (to maximize testing potential with mocks, etc). In this way, we are able to give the test developer fine control of what is tested. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I've used a ChatGPT example that generates the same error in the original code.
🧨 NOTE: The linking error is intermittent; sometimes the build works correctly, and sometimes it doesn't. It's unclear why the error occurs occasionally and then resolves itself at other times.
1. Set Up the Project
First, install Ceedling and create a new project:
bash
gem install ceedling
ceedling new project_name
cd project_name
This will create a basic Ceedling project structure.
2. Create C Files
Let's assume you have 4 C files:
file1.c
file2.c
file3.c
file4.c
Each file will contain simple functions, and you'll link them together.
file1.c
#include "file1.h"
int function_file1() {
return 1;
}
file1.h
#ifndef FILE1_H
#define FILE1_H
int function_file1();
#endif
file2.c
#include "file2.h"
#include "file1.h"
int function_file2() {
return function_file1() + 2;
}
file2.h
#ifndef FILE2_H
#define FILE2_H
int function_file2();
#endif
file3.c
#include "file3.h"
#include "file2.h"
int function_file3() {
return function_file2() + 3;
}
file3.h
#ifndef FILE3_H
#define FILE3_H
int function_file3();
#endif
file4.c
#include "file4.h"
#include "file3.h"
int function_file4() {
return function_file3() + 4;
}
file4.h
#ifndef FILE4_H
#define FILE4_H
int function_file4();
#endif
3. Write Tests
Now, let's create tests for these functions. You can place your test files in the test folder. We'll write one test file that tests the linked functions.
test_file1.c
#include "unity.h"
#include "file1.h"
#include "file2.h"
#include "file3.h"
#include "file4.h"
void setUp(void) {
// Code to set up before each test (if needed)
}
void tearDown(void) {
// Code to clean up after each test (if needed)
}
void test_function_file1(void) {
TEST_ASSERT_EQUAL(1, function_file1());
}
void test_function_file2(void) {
TEST_ASSERT_EQUAL(3, function_file2());
}
void test_function_file3(void) {
TEST_ASSERT_EQUAL(6, function_file3());
}
void test_function_file4(void) {
TEST_ASSERT_EQUAL(10, function_file4());
}
4. Configure Ceedling to Include All Files
In the project.yml file, you need to make sure all C files are included in the build process. By default, Ceedling should include files from the src directory, but you may need to ensure the test files are linked properly.
Make sure the :test_files section in project.yml is correctly set:
test_files:
5. Run Tests
After everything is set up, run the Ceedling tests:
ceedling test:all
Expected Output
Ceedling should compile the files and run the tests, showing you something like:
Test 'test_function_file1' passed.
Test 'test_function_file2' passed.
Test 'test_function_file3' passed.
Test 'test_function_file4' passed.
The actual Result:
🧨 EXCEPTION: 'Default Test Linker' (gcc) terminated with exit code [1] and output >>
/usr/bin/ld: build/test/out/test_file1_2/file2.o: in function
function_file2': /home/Z/Code/Test_Linker/src/file2.c:5:(.text+0xe): undefined reference tofunction_file1'collect2: error: ld returned 1 exit status
🌱 Ceedling could not complete operations because of errors
Beta Was this translation helpful? Give feedback.
All reactions