Undefined reference to ´[function]' - c

Edit #1: Please find the solution as an answer to this initial post as a code example is used. Please try also to find a possible solution in What is an undefined reference/unresolved external symbol error and how do I fix it? It didn't help me, but maybe it's suitable for you.
I try to build a project which consists of each two header and application files. If I compile each file, no errors occure. If I build the project, I run into the below error.
cd 'D:\Master\M_32561\9000_A\B13-03'
P:\PortableApps\MinGW\msys\1.0\bin\make.exe -f Makefile CONF=Debug
"/P/PortableApps/MinGW/msys/1.0/bin/make.exe" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make.exe[1]: Entering directory `/d/Master/M_32561/9000_A/B13-03'
"/P/PortableApps/MinGW/msys/1.0/bin/make.exe" -f nbproject/Makefile-Debug.mk dist/Debug/MinGW-Windows/b13-03.exe
make.exe[2]: Entering directory `/d/Master/M_32561/9000_A/B13-03'
mkdir -p dist/Debug/MinGW-Windows
gcc -o dist/Debug/MinGW-Windows/b13-03 build/Debug/MinGW-Windows/B13-03_F1.o build/Debug/MinGW-Windows/B13-03_MAIN.o
build/Debug/MinGW-Windows/B13-03_F1.o: In function `anzeigen_artikelbestand':
D:\Master\M_32561\9000_A\B13-03/B13-03_F1.c:283: undefined reference to `ausgeben_artikelbestand_mit_listenkopf'
build/Debug/MinGW-Windows/B13-03_MAIN.o: In function `main':
D:\Master\M_32561\9000_A\B13-03/B13-03_MAIN.c:39: undefined reference to `erfassen_artikel'
D:\Master\M_32561\9000_A\B13-03/B13-03_MAIN.c:42: undefined reference to `anzeigen_artikel'
D:\Master\M_32561\9000_A\B13-03/B13-03_MAIN.c:45: undefined reference to `aendern_artikel'
D:\Master\M_32561\9000_A\B13-03/B13-03_MAIN.c:48: undefined reference to `loeschen_artikel'
collect2.exe: error: ld returned 1 exit status
make.exe[2]: *** [dist/Debug/MinGW-Windows/b13-03.exe] Error 1
make.exe[2]: Leaving directory `/d/Master/M_32561/9000_A/B13-03'
make.exe[1]: *** [.build-conf] Error 2
make.exe[1]: Leaving directory `/d/Master/M_32561/9000_A/B13-03'
make.exe": *** [.build-impl] Error 2
The project consists of the following files:
B13-03_MAIN.h
B13-03_MAIN.c
B13-03_H1.h
B13-03_F1.c
The undefined functions are declared in B13-03_MAIN.h and defined in B13-03_MAIN.c. Both application files are using B13-03_MAIN.h as a prototype.
Thanks for any help is much aprreciated.

The issue dealt with an incorrect nesting of the undefined functions. I focus on B13-03_MAIN.h and B13-03_MAIN.c.
/* B13-03_MAIN.h */
create_article();
show_article();
edit_article();
delete_article();
Implementation w/ issue:
/* B13-03_MAIN.c */
#include <stdio.h>
#include "B13-03_MAIN.h"
#include "B13-03_H1.h"
void main(void) {
switch(option) {
case abc_c: create_article(); break;
case abc_s: show_article(); break;
case abc_e: edit_article(); break;
case abc_d: delete_article(); break;
default: printf("Undefined option.");
}
void create_article(void) {
...
}
void show_article(void) {
...
}
void edit_article(void) {
...
}
void delete_article(void) {
...
}
} /* This curly braces is incorrectly nested */
Implementation w/o issues:
void main(void) {
switch(option) {
case abc_c: create_article(); break;
case abc_s: show_article(); break;
case abc_e: edit_article(); break;
case abc_d: delete_article(); break;
default: printf("Undefined option.");
}
} /*Incorrectly nested curly brace should be implemented here */
void create_article(void) {
...
}
void show_article(void) {
...
}
void edit_article(void) {
...
}
void delete_article(void) {
...
}

Related

Linux RT-Unable to link .lib to VSCode-Undefined reference to `DAQmxCreateTask'

I was trying to cross compile a C code with CMake inside Linux RT using VSCode. But I am encountering an error due to the mistake in linking (.so) file with project. I have gone through many solutions but failed to run the task. My code is given below:
#include<stdio.h>
#include"/home/admin/helloworld/src/NIDAQmx.h"
TaskHandle taskHandle=0;
int ret=0;
void main()
{
printf("Hello world");
ret=DAQmxCreateTask("task",&taskHandle);
printf("Return for creating task is %d\n",ret);
DAQmxStopTask (taskHandle);
DAQmxClearTask(taskHandle);
printf("Task closed ");
}
Error while running the task.
[ 50%] Linking C executable bin/helloWorld
CMakeFiles/helloWorld.dir/home/admin/helloworld/src/helloWorld.c.o:
In function `main':/home/admin/helloworld/src/helloWorld.c:11: undefined reference to `DAQmxCreateTask'
/home/admin/helloworld/src/helloWorld.c:13: undefined reference to `DAQmxStopTask'
/home/admin/helloworld/src/helloWorld.c:14: undefined reference to `DAQmxClearTask'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/helloWorld.dir/build.make:95: bin/helloWorld] Error 1
make[1]: *** [CMakeFiles/Makefile2:68:CMakeFiles/helloWorld.dir/all] Error 2
* The terminal process
"/bin/bash '-c', 'make'" failed to launch
(exit code: 2).
* Terminal will be reused by tasks, press any key to close it.
I modified my CMakeLists.txt as follows:
cmake_minimum_required(VERSION 3.7.2)
# project settings
project(helloWorld VERSION 0.1.0)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY bin)
set(CMAKE_GENERATOR "Unix Makefiles")
# executable settings
add_executable(helloWorld ../src/helloWorld.c)
set(CMAKE_BUILD_TYPE Debug)
LINK_LIBRARIES(NIDAQmx ../src/libnidaqmx.so)
If I remove the elements associated with NIDAQmx code working properly.

"Undefined reference to" file which is not on the current direcory

I met error pintos/src/userprog/build/../../userprog/syscall.c:56: undefined reference to `exit' while working on a pintos project.
Below is part of directory tree that is related with error.
-lib
-user
-syscall.c
-syscall.h
-userprog
-syscall.c
(lib and userprog have same hierarchy.)
In userprog/syscall.c, The function exit is well-defined on both header file and c file.
// lib/user/syscall.c
void
exit (int status)
{
syscall1 (SYS_EXIT, status);
NOT_REACHED ();
}
// lib/user/syscall.h
void exit (int status) NO_RETURN;
But When I call exit on userprog/syscall.c, I meet error undefined refrence to 'exit'
I includede syscall.h in lib/user/ as #include "lib/user/syscall.h"
// userprog/syscall.c
#include "lib/user/syscall.h"
void
foo()
{
...
exit(-1);
}
What would be the problem?

set_target_properties does not work with multiple --wrap functions

#include <stdarg.h>
#include <setjmp.h>
#include <stddef.h>
#include <cmocka.h>
#include "stdint.h"
typedef enum {
eSTATE_STARTUP,
eSTATE_ADDRESSING,
eSTATE_RANDOMISE,
eSTATE_SELECTED
}e_State_t;
void test_main()
{
static e_State_t e_state = eSTATE_STARTUP;
if(get_rx_dali_flag())
{
application_process(&e_state);
}
switch(e_state)
{
case eSTATE_STARTUP:
{
set_led_frequency(0);
break;
}
case eSTATE_ADDRESSING:
{
set_led_frequency(1);
break;
}
case eSTATE_RANDOMISE:
{
set_led_frequency(10);
break;
}
case eSTATE_SELECTED:
{
set_led_frequency(100);
break;
}
default:
break;
}
}
int __wrap_get_rx_dali_flag(void)
{
int data_available = mock_type(uint8_t);
return data_available;
}
void __wrap_application_process(e_State_t * ptr_state)
{
check_expected_ptr(ptr_state);
*ptr_state = mock_type(e_State_t);
}
void __wrap_set_led_frequency(uint16_t frequency)
{
assert_int_equal(frequency, 1);
}
void main_should_setLedFreq_1_when_stateADDRESSING()
{
will_return(__wrap_get_rx_dali_flag, 1);
will_return(__wrap_application_process, eSTATE_ADDRESSING);
test_main();
}
int main()
{
const struct CMUnitTest tests[] =
{
cmocka_unit_test(main_should_setLedFreq_1_when_stateADDRESSING),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
cmake_minimum_required(VERSION 3.21 FATAL_ERROR)
SET(CMAKE_C_COMPILER "C:/msys64/mingw64/bin/gcc.exe")
SET(GCC_COVERAGE_COMPILE_FLAGS "-g")
project(beTheLight-unit-tests VERSION 1.0 LANGUAGES C)
include(cmake/FetchCMocka.cmake)
add_executable(beTheLight-unit-tests main.c)
target_compile_features(beTheLight-unit-tests PRIVATE c_std_99)
target_link_libraries(beTheLight-unit-tests PRIVATE cmocka-static)
enable_testing()
add_test(NAME beTheLight-unit-tests COMMAND beTheLight-unit-tests)
set_target_properties(beTheLight-unit-tests PROPERTIES LINK_FLAGS "-Wl, --wrap=get_rx_dali_flag, --wrap=set_led_frequency, --wrap=application_process")
==============================================================
[ 50%] Linking C executable beTheLight-unit-tests.exe
gcc.exe: error: unrecognized command-line option '--wrap=get_rx_dali_flag,'
gcc.exe: error: unrecognized command-line option '--wrap=set_led_frequency,'
gcc.exe: error: unrecognized command-line option '--wrap=application_process'
make[2]: *** [beTheLight-unit-tests.exe] Error 1
make[1]: *** [CMakeFiles/beTheLight-unit-tests.dir/all] Error 2
make: *** [all] Error 2
I am trying to get cmocka unit testing to work but for some reason the linker does not accept multiple --wrap linker flags. I have seen people doing it this way at another post in stackoverflow. I am using gcc version 10.3.0 built by MSYS2 project on win10.
Just use:
add_executable(beTheLight-unit-tests main.c)
target_link_options(beTheLight-unit-tests PUBLIC
-Wl,--wrap=get_rx_dali_flag
-Wl,--wrap=set_led_frequency
-Wl,--wrap=application_process
)
on older cmake use target_link_libraries. No need for set_target_properties.

Why does a function declared and included using header file show as undefined?

I have the following code in my files and all of these are in the same directory:
readMake.c:
#include <stdio.h>
#include "my_make.h"
int main(int argc, char* argv[]) {
printSomething();
}
printer.c:
#include <stdio.h>
int printSomething(){
printf("printed\n");
return 1;
}
my_make.h:
int printSomething();
However, when I try to compile and run my code, I get the following error:
/usr/lib/gcc/x86_64-pc-cygwin/11/../../../../x86_64-pc-cygwin/bin/ld: CMakeFiles/prob1.dir/readMake.c.o: in function `main':
/cygdrive/c/Users/harsh/OneDrive/Documents/CSC352/assg9/prob1/readMake.c:5: undefined reference to `printSomething'
collect2: error: ld returned 1 exit status
make[3]: *** [CMakeFiles/prob1.dir/build.make:93: prob1.exe] Error 1
make[2]: *** [CMakeFiles/Makefile2:83: CMakeFiles/prob1.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:90: CMakeFiles/prob1.dir/rule] Error 2
make: *** [Makefile:124: prob1] Error 2
My IDE shows the proper signature of the printSignature function and where it is declared in the header file when I hover over the call in main, but the compilation doesn't seem to be working. Why is this happening and how can I resolve this?
I figured out a solution specific to CLion, unloading the CMakeLists.txt file and then creating a new one seems to resolve the issue.
I had to do 'Tools>CMake>Unload'
and then create a new CMakeLists.txt file which was automatically done by clicking an option appearing at the top of the window and selecting all of the files from a list of checkboxes.

Netbeans C program return value 2

I just got my Netbeans tools configured,and (finally) it worked :) (I checked with Hello World). We were shifting so I have had like a 3 month gap so I though I'd make a calculator rather than some other program like a palindrome checker and bleh bleh bleh.
So here's the code :
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
void addition(int,int);
void subtraction(int,int);
void mutiplication(int,int);
void division(int,int);
int main() {
int x,y,choice,redo = 1;
while(redo)
{
printf("\nWelcome to the CalC :D\nPlease make a choice\n1.Addition\n2.Subtraction\n3.Multiplication\n4.Division\n>");
scanf("%d",&choice);
switch(choice);
{
case '1' :
{
printf("Enter the first number\n>");
scanf("%d",&x);
printf("\nThe second number?\n>");
scanf("%d",&y);
addition(x,y);
}
case '2' :
{
printf("Enter the first number\n>");
scanf("%d",&x);
printf("\nThe number to be subtracted from %d is?\n>",x);
scanf("%d",&y);
subtraction(x,y);
}
case '3' :
{
printf("Enter the first number\n>");
scanf("%d",&x);
printf("\nThe number to be multiplied with %d is?\n>",x);
scanf("%d",&y);
multiplication(x,y);
}
case '4' :
{
printf("Enter the first number\n>");
scanf("%d",&x);
printf("\nThe number to be divided by %d is?\n>",x);
scanf("%d",&y);
division(x,y);
}
}
printf("\nWould you like to make another calculation?\n1.Yes '_' \n2.No! :p\n>");
scanf("%d",&redo);
}
return (EXIT_SUCCESS);
}
void addition(int x,int y)
{
int sum;
sum = x + y;
printf("\nThe sum of %d and %d is %d\n(Press enter to display the menu)",x,y,sum);
getch();
}
void subtraction(int x,int y)
{
int difference;
ce;
difference = x - y;
printf("The difference between %d and %d is %d\n(Press enter to display the menu)",x,y,difference);
getch();
}
void multiplication(int x,int y)
{
int product;
product = x * y;
printf("The product of %d and %d is %d\n(Press enter to display the menu)",x,y,product);
getch();
}
void division(int x,int y)
{
float quotent;
quotent = (float)x/(float)y;
printf("The quotient of %d and %d is %.2f\n(Press enter to display the menu)",x,y,quotent);
getch();
}
And this is the error I get :
"/C/MinGW/bin/make.exe" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory 'C:/Users/CaptFuzzyboots/Documents/NetBeansProjects/CalC'
"c:/MinGW/bin/make.exe" -f nbproject/Makefile-Debug.mk dist/Debug/MinGW-Windows/calc.exe
make[2]: Entering directory 'C:/Users/CaptFuzzyboots/Documents/NetBeansProjects/CalC'
mkdir -p build/Debug/MinGW-Windows
rm -f build/Debug/MinGW-Windows/main.o.d
gcc -c -g -MMD -MP -MF build/Debug/MinGW-Windows/main.o.d -o build/Debug/MinGW-Windows/main.o main.c
main.c: In function 'main':
main.c:26:5: error: case label not within a switch statement
main.c:34:9: error: case label not within a switch statement
main.c:42:9: error: case label not within a switch statement
main.c:52:9: error: case label not within a switch statement
main.c: In function 'subtraction':
main.c:78:5: error: 'ce' undeclared (first use in this function)
main.c:78:5: note: each undeclared identifier is reported only once for each function it appears in
main.c: At top level:
main.c:83:6: warning: conflicting types for 'multiplication' [enabled by default]
main.c:48:13: note: previous implicit declaration of 'multiplication' was here
nbproject/Makefile-Debug.mk:66: recipe for target 'build/Debug/MinGW-Windows/main.o' failed
make[2]: *** [build/Debug/MinGW-Windows/main.o] Error 1
make[2]: Leaving directory 'C:/Users/CaptFuzzyboots/Documents/NetBeansProjects/CalC'
nbproject/Makefile-Debug.mk:59: recipe for target '.build-conf' failed
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory 'C:/Users/CaptFuzzyboots/Documents/NetBeansProjects/CalC'
nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' failed
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 661ms)
I'm used to code::blocks so I don't really know what's the problem here :\
please help me out,I clearly defined the switch labels as '1','2','3','4'!
Thanks guys :)
You have an extra semicolon after switch(choice). Your code has
switch(choice);
and it should be
switch(choice)
A few other problems:
1) You misspelled multiplication when you defined the function at the top.
2) There's a stray ce; in your subtraction function.
3) You don't have any break statements in your case statements (it looks like you don't want the cases to fall through, so you probably need them)
4) choice is an int, but you're casing on chars. Since you're reading ints with scanf, you probably want to have 1,2,3,4 instead of '1','2','3','4'.
Your main issue is the extra semi-colon after switch (choice). Remove that semi-colon, and be sure to have a break after each case as well.
You also have choice declared as an integer, but your case labels are switching on a char; it would be advisable to get rid of the single quotes around each case.
do case '1' instead of case 1
write multiplication and not mutiplication
remove the "ce" from the subtraction function
remove semicolon after switch(choice)
have a 'break' in each case
at the begining, correct spelling of multiplication
remove "ce;" inside the function subtraction.
Change cases '1', '2', etc to 1, 2...
Please note that, running a normal C code from Netbeans IDE should produce the same result if you run it in Eclipse or some other IDE. Your error is noway related to Netbeans.

Resources