Undefined reference error [duplicate] - c

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 6 years ago.
EDIT: A complete duplicate of course, isn't the whole point of stackoverflow to ask questions, and get specific answers, the 'answer' you linked me to is:
A. For C++, so partly unintelligible
B. Reads like a wiki article, it is massive, and not specific in the slightest, or in the least bit helpful.
I have copied an example bit of code from the examples folder in the code library (libftdi) I downloaded, to where I want to write my project, when I run it, it has a lot of undefined references, however it runs fine in its original location.
Main file:
#include <stdio.h>
#include <stdlib.h>
#include <ftdi.h>
int main(void)
{
int ret, i;
struct ftdi_context *ftdi;
struct ftdi_device_list *devlist, *curdev;
char manufacturer[128], description[128];
int retval = EXIT_SUCCESS;
if ((ftdi = ftdi_new()) == 0)
{
fprintf(stderr, "ftdi_new failed\n");
return EXIT_FAILURE;
}
if ((ret = ftdi_usb_find_all(ftdi, &devlist, 0, 0)) < 0)
{
fprintf(stderr, "ftdi_usb_find_all failed: %d (%s)\n", ret, ftdi_get_error_string(ftdi));
retval = EXIT_FAILURE;
goto do_deinit;
}
printf("Number of FTDI devices found: %d\n", ret);
i = 0;
for (curdev = devlist; curdev != NULL; i++)
{
printf("Checking device: %d\n", i);
if ((ret = ftdi_usb_get_strings(ftdi, curdev->dev, manufacturer, 128, description, 128, NULL, 0)) < 0)
{
fprintf(stderr, "ftdi_usb_get_strings failed: %d (%s)\n", ret, ftdi_get_error_string(ftdi));
retval = EXIT_FAILURE;
goto done;
}
printf("Manufacturer: %s, Description: %s\n\n", manufacturer, description);
curdev = curdev->next;
}
done:
ftdi_list_free(&devlist);
do_deinit:
ftdi_free(ftdi);
return retval;
}
Error:
ollieb#ursus ~/Documents/BitBang $ make
make bitbang
make[1]: Entering directory '/home/ollieb/Documents/BitBang'
cc -Wall -g -I/usr/include/libftdi1/ bitbang.c -o bitbang
/tmp/cc20kltT.o: In function `main':
/home/ollieb/Documents/BitBang/bitbang.c:13: undefined reference to `ftdi_new'
/home/ollieb/Documents/BitBang/bitbang.c:19: undefined reference to `ftdi_usb_find_all'
/home/ollieb/Documents/BitBang/bitbang.c:21: undefined reference to `ftdi_get_error_string'
/home/ollieb/Documents/BitBang/bitbang.c:32: undefined reference to `ftdi_usb_get_strings'
/home/ollieb/Documents/BitBang/bitbang.c:34: undefined reference to `ftdi_get_error_string'
/home/ollieb/Documents/BitBang/bitbang.c:42: undefined reference to `ftdi_list_free'
/home/ollieb/Documents/BitBang/bitbang.c:44: undefined reference to `ftdi_free'
collect2: error: ld returned 1 exit status
<builtin>: recipe for target 'bitbang' failed
make[1]: *** [bitbang] Error 1
make[1]: Leaving directory '/home/ollieb/Documents/BitBang'
Makefile:4: recipe for target 'all' failed
make: *** [all] Error 2
Make file:
CFLAGS=-Wall -g -I/usr/include/libftdi1/
all:
make bitbang
clean:
rm bitbang

You have to link against the ftdi library (-Llibdirectory -llib)

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.

Embedded C in SystemTap - dereferencing pointer to incomplete type

I am following this tutorial: https://blog.lexfo.fr/cve-2017-11176-linux-kernel-exploitation-part1.html
As I try to see what the netlink_sock contains in state, I use this embedded C code:
%{
#include <net/sock.h>
#include <linux/netlink.h>
%}
function dump_netlink_sock:long (arg_sock:long)
%{
struct sock *sk = (void*) STAP_ARG_arg_sock;
struct netlink_sock * nlk = (void*) sk;
_stp_printf("-={ dump_netlink_sock: %p }=-\n", nlk);
_stp_printf("- sk = %p\n", sk);
_stp_printf("- sk->sk_rmem_alloc = %d\n", sk->sk_rmem_alloc);
_stp_printf("- sk->sk_rcvbuf = %d\n", sk->sk_rcvbuf);
_stp_printf("- sk->sk_refcnt = %d\n", sk->sk_refcnt);
_stp_printf("- nlk->state = %x\n", (nlk->state & 0x1));
_stp_printf("-={ dump_netlink_sock: END}=-\n");
%}
probe kernel.function("netlink_attachskb")
{
if (execname() == "exploit")
{
printf("(%d - %d) >>> netlink_attachskb (%s)\n", pid(), tid(), $$parms)
}
dump_netlink_sock($sk);
}
I made sure by myself in the Linux kernel source code - state exists in netlink_sock.
This is my result:
shahar#debian:~/exploitation$ sudo stap -v -g mq_notify.stp
[sudo] password for shahar:
Pass 1: parsed user script and 95 library script(s) using 83352virt/28420res/4880shr/24252data kb, in 0usr/80sys/78real ms.
Pass 2: analyzed script: 699 probe(s), 15 function(s), 5 embed(s), 0 global(s) using 278348virt/102604res/6696shr/97036data kb, in 420usr/730sys/1153real ms.
Pass 3: translated to C into "/tmp/stapFmyHer/stap_cc49251867b5bd20ade8fc721d5f8895_209103_src.c" using 275848virt/102252res/6468shr/97036data kb, in 20usr/10sys/33real ms.
/tmp/stapFmyHer/stap_cc49251867b5bd20ade8fc721d5f8895_209103_src.c: In function ‘function_dump_netlink_sock’:
/tmp/stapFmyHer/stap_cc49251867b5bd20ade8fc721d5f8895_209103_src.c:2517:41: error: dereferencing pointer to incomplete type
_stp_printf("- nlk->state = %x\n", (nlk->state & 0x1));
^
make[3]: *** [/tmp/stapFmyHer/stap_cc49251867b5bd20ade8fc721d5f8895_209103_src.o] Error 1
make[2]: *** [_module_/tmp/stapFmyHer] Error 2
make[1]: *** [sub-make] Error 2
make: *** [all] Error 2
WARNING: kbuild exited with status: 2
Pass 4: compiled C into "stap_cc49251867b5bd20ade8fc721d5f8895_209103.ko" in 130usr/380sys/740real ms.
Pass 4: compilation failed. [man error::pass4]
Tip: /usr/share/doc/systemtap/README.Debian should help you get started.
In addition, I tried to create my own struct (basically copied netlink_sock from Linux source, but I could not get it compiled - I am not sure where to place my struct in the .stp file.

Undefined symbols for architecture x86_64 and linker command failed with exit code 1

I run the following code from APUE
#include "apue.h"
#include <sys/wait.h>
void pr_exit(int status)
{
if (WIFEXITED(status))
printf("normal termination, exit status = %d\n",
WEXITSTATUS(status));
else if (WIFSIGNALED(status))
printf("abnormal termination, signal number = %d%s\n",
WTERMSIG(status),
#ifdef WCOREDUMP
WCOREDUMP(status) ? " (core file generated)" : "");
#else
"");
#endif
else if (WIFSTOPPED(status))
printf("child stopped, signal number = %d\n",
WSTOPSIG(status));
}
but get error:
$ cc my_wait.c
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I checked multiple times and ensure there no difference with the book's instruction ..
How could I solve the problem?
Transferring a comment into an answer, as requested.
The error message says "there is no function main()", and the source code you show has no function main(), so there's minimal surprise that there's that error message.
Where did you think main() was going to come from?
When you build a program, there needs to be a main() from somewhere, and the standard C library does not provide an implementation. (If you work with Flex or Lex, or Bison or Yacc, you may find minimal main() programs in their libraries, but these are an exception, not the rule.)

C : Including Header file with .dll file and .lib file on Linux

I have a three files, Oracle.h, Oracle.dll, Oracle.lib.
These files are in same directory of main.c file.
And I want to include Oracle.h file.
#include <stdio.h>
#include "Oracle.h"
#pragma comment(lib, "Oracle.lib")
int main(void)
{
int crit;
char P[36] = { 0, };
char C[36] = { 0, };
int Clen = 36;
crit = Oracle(C, Clen);
printf("%d", crit);
return 0;
}
I used this code for main.c file, and compile by make main command.
But compiling this file has error, and I don't know what should I do.
$ make problem4_oracle
cc problem4_oracle.c -o problem4_oracle
/var/tmp/cc9oUwU3.o: In function main':
problem4_oracle.c:(.text+0x79): undefined reference to Oracle'
collect2: error: ld returned 1 exit status
<builtin>: recipe for target 'problem4_oracle' failed
make: *** [problem4_oracle] Error 1
Please help me.

Error_handler… what?

I'm trying to learn some C and have chosen to use the book "SAMS Teach yourself C in 21 days". ( btw is there any good C book at all? This is my third! I always end up with bad or broken code that is supposed to work and hit a wall in my learning process when it doesn't! :-( )
Sadly I've run upon the code underneath that I just should type in and run. The typing went well but the running... well, not so well!
The compiler gives me an error about this line:
{
printf ("Printer busy or disconnected\n"); error_handler; }
when I try to run this code. And since I'm VERY MUCH a novice when it comes to C coding, I have NO IDEA at all what to do, when the editor returns an error messages like this:
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .clean-conf
rm -f -r build/Debug
rm -f dist/Debug/GNU-MacOSX/type_and_run
CLEAN SUCCESSFUL (total time: 158ms)
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
"/usr/bin/make" -f nbproject/Makefile-Debug.mk dist/Debug/GNU-MacOSX/type_and_run
mkdir -p build/Debug/GNU-MacOSX
rm -f build/Debug/GNU-MacOSX/Type and run 1.o.d
gcc -c -g -MMD -MP -MF build/Debug/GNU-MacOSX/Type and run 1.o.d -o build/Debug
/GNU-MacOSX/Type\ and\ run\ 1.o Type\ and\ run\ 1.c
i686-apple-darwin10-gcc-4.2.1: and: No such file or directory
i686-apple-darwin10-gcc-4.2.1: run: No such file or directory
i686-apple-darwin10-gcc-4.2.1: 1.o.d: No such file or directory
Type and run 1.c: In function 'do_heading':
Type and run 1.c:54: error: 'error_handler' undeclared (first use in this function)
Type and run 1.c:54: error: (Each undeclared identifier is reported only once
Type and run 1.c:54: error: for each function it appears in.)
make[2]: *** [build/Debug/GNU-MacOSX/Type and run 1.o] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 158ms)
All I can understand is that "error_handler" is undeclared and that it does not seem to be a library function. SIGH!
So said short, is there anything I can do to modify the code so it will work as intended and I can move on?
Or should I just throw the book over in the pile of useless books about C?
Here is the code in its full length:
void do_heading(char *filename);
int line = 0, page = 0;
int main(int argv, char *argc[])
{
char buffer[256];
FILE *fp;
if (argv < 2)
{
fprintf(stderr, "\nProper Usage is: " );
fprintf(stderr, "\n\nprint_it filename.ext\n");
return (1);
}
if ((fp = fopen(argc[1], "r")) == NULL)
{
fprintf(stderr, "Error opening file, %s!", argc[1]);
return (1);
}
page = 0;
line = 1;
do_heading(argc[1]);
while( fgets( buffer, 256, fp ) != NULL )
{
if( line % 55 == 0 )
do_heading(argc[1]);
fprintf( stdout , "%4d:\t%s", line++, buffer );
}
fprintf( stdout , "\f" );
fclose(fp);
return 0;
}
void do_heading(char *filename)
{
page++;
if (page > 1)
fprintf( stdout , "\f");
fprintf( stdout, "Page: %d, %s\n\n", page, filename);
if ((stdout = fopen("PRT:","w")) == NULL) {
printf ("Printer busy or disconnected\n"); error_handler; }
}
Try this book:
C Programming Language (2nd Edition) by Brian W. Kernighan, Dennis M. Ritchie.
You have argv and argc swapped.
if (argv 1)
Should be:
if (argc == 1)
You can simply remove the error_handler chunk as it seems to serve no purpose and is not declared in the code. What the book was likely trying to tell you was to fprintf to stderror perhaps?
So the compiler output might look intimidating, but, as a beginner, the bit you care about is usually in English. This example is no exception:
Type and run 1.c: In function 'do_heading':
Type and run 1.c:54: error: 'error_handler' undeclared (first use in this function)
Type and run 1.c:54: error: (Each undeclared identifier is reported only once
Type and run 1.c:54: error: for each function it appears in.)
It's telling you that error_handler is undeclared. It appears nowhere else in your program so there's no way you're able to use it here. Let's look at the line:
if ((stdout = fopen("PRT:","w")) == NULL) {
printf ("Printer busy or disconnected\n"); error_handler; }
You should know that semicolons are one way to indicate the end of a statement and that good practice is to put just one statement on a line. Break it up:
if ((stdout = fopen("PRT:","w")) == NULL) {
printf ("Printer busy or disconnected\n");
error_handler;
}
Now it's obvious: you're just saying error_handler;; it's not doing anything, it's not a function call (no parentheses), not a variable you're assigning to or reading from so it's clearly a huge fat error. Delete it and try again.
By the way, if the line appeared in the book exactly as you've put it here - with two semicolons on a line - then yes, you should throw it on the fire. Read Kernighan and Ritchie's "The C Programming Language" and do all the exercises in it - you will be amazed how quickly you go from knowing nothing to being ready to work on real programs.

Resources