Skip to content

Commit 0c89d62

Browse files
fix(core): Ensure correct function linkage for C/C++ compatibility
This commit fixes a critical linkage bug that caused "undefined reference" errors when compiling with a C++ compiler (G++), even though the same code compiled successfully with a C compiler (GCC). The root cause is a divergence in how `inline` is handled by standard C++ versus the common GNU C compiler dialect. In C++, an `inline` function's symbol is not guaranteed to be emitted if all its uses within a translation unit are successfully inlined. This caused other object files, which needed to call the function, to fail at the linking stage. The C compilation was succeeding due to a non-standard GCC extension where a plain `inline` definition also generates a linkable external symbol. This behavior is not guaranteed by the C99/C11 standards and masked the portability issue. By removing the `inline` keyword from the function definition inside the `#ifdef TEC_IMPL` block, the function becomes a standard, non-inline function. This guarantees that one globally visible symbol is generated, ensuring correct and portable linking for both C and C++ compilers. REFERENCES: - https://en.cppreference.com/w/c/language/inline.html - https://en.cppreference.com/w/cpp/language/inline.html - https://gcc.gnu.org/onlinedocs/gcc/Inline.html Signed-off-by: Shashwat Agrawal <shashwatagrawal473@gmail.com>
1 parent da4d548 commit 0c89d62

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

CMakeLists.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
cmake_minimum_required(VERSION 3.10)
2+
project(tec.h C)
3+
4+
set(CMAKE_C_STANDARD 11)
5+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -pedantic")
6+
7+
include_directories(include)
8+
9+
file(GLOB_RECURSE SRC_FILES "src/*.c")
10+
11+
file(GLOB_RECURSE TEST_SOURCES "tests/*.c")
12+
13+
list(FILTER SRC_FILES EXCLUDE REGEX ".*/main\\.c$")
14+
15+
add_executable(main src/main.c ${SRC_FILES})
16+
add_executable(test_runner ${TEST_SOURCES} ${SRC_FILES})
17+
18+
add_custom_target(test
19+
COMMAND test_runner
20+
DEPENDS test_runner
21+
COMMENT "Running tests..."
22+
)

tec.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ extern "C" {
564564

565565
tec_context_t tec_context = {};
566566

567-
inline void TEC_POST_FAIL(void) {
567+
void TEC_POST_FAIL(void) {
568568
tec_context.current_failed++;
569569
tec_context.stats.failed_assertions++;
570570
#ifdef __cplusplus
@@ -575,7 +575,7 @@ inline void TEC_POST_FAIL(void) {
575575
#endif
576576
}
577577

578-
inline void _tec_skip_impl(const char *reason, int line) {
578+
void _tec_skip_impl(const char *reason, int line) {
579579
const char *_reason = (reason);
580580
snprintf(tec_context.failure_message, TEC_MAX_FAILURE_MESSAGE_LEN,
581581
TEC_PRE_SPACE TEC_YELLOW TEC_ARROW_CHAR TEC_RESET

0 commit comments

Comments
 (0)