I am getting an undefined reference error when trying to link my application with code blocks and command line gcc. The file giving the error has the function prototype in the header. The source file the function the linker is complaining about has other functions in it that aren't giving this error. I copied and pasted the function declarator to the header and calling function (fixing the parameters of course) to ensure it is not a typo. What am I missing? Other functions called from linked_list.c don't give me linker errors.
Here are the code snippets:
mw_proxy.h:
struct addr_l_node
{
uint32_t address;
struct addr_l_node *next;
};
...
typedef struct addr_l_node addr_l_node;
...
void delete_addr_list(addr_l_node *first);
parse_config.c:
#include "mw_proxy.h"
...
addr_l_node *first;
...
delete_addr_list(first);
linked_list.c:
void delete_addr_list(addr_l_node *first)
{
...
}
Finally I am getting this error from code blocks:
obj/Debug/parse_config.o||In function `parse_config':|
/media/sf_H_DRIVE/src/codeblocks/mw_proxy/parse_config.c|240|undefined reference to `delete_addr_list'|
||=== Build finished: 1 errors, 0 warnings ===|
From a bash shell:
gcc int_array.o cleanup.o check_datagram.o mw_proxy.o parse_config.o linked_list.o
parse_config.o: In function `parse_config':
/media/sf_H_DRIVE/src/codeblocks/mw_proxy/parse_config.c:240: undefined reference to `delete_addr_list'
collect2: error: ld returned 1 exit status
Related
I'm trying to compile a userspace C sourcefile which uses i2c_smbus_read_* and i2c_smbus_write_* functions in a Raspberry Pi 3B+ with kernel 5.10.63-v7+. It should be possible, according to this Linux kernel document. I already installed libi2c-dev.
If I use:
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include <i2c/smbus.h>
and I run gcc myfile.c -o myfile, I get:
/usr/bin/ld: /tmp/ccIWkqsK.o: in function `i2c_read':
myfile.c:(.text+0x84): undefined reference to `i2c_smbus_read_byte_data'
/usr/bin/ld: /tmp/ccIWkqsK.o: in function `i2c_write':
myfile.c:(.text+0x160): undefined reference to `i2c_smbus_write_block_data'
collect2: error: ld returned 1 exit status
Which could it be the problem?
Note that these functions are present in my /usr/include/i2c/smbus.h:
...
extern __s32 i2c_smbus_read_byte_data(int file, __u8 command);
extern __s32 i2c_smbus_write_byte_data(int file, __u8 command, __u8 value);
extern __s32 i2c_smbus_read_word_data(int file, __u8 command);
...
I agree you don't need extern "C"in a C program. Check your headers and see if your header defines the function as inline or not. If it only declares it, you need to find out which library is the function defined in and link the library in your program.
Edit: I see you already updated the question. In this case you need to link the i2c library similar to what they do here. Just add a parameter to your gcc command: -li2c.
I am trying to compile a c program with a static library and its not working .
This is the error :
undefined reference to `calculatearea'
collect2.exe: error: ld returned 1 exit status .
The static files were made with the gcc / g++ compilers .
This is the main code :
#include <stdio.h>
#include <stdint.h>
int calculatearea(int a , int b);
int main()
{
int c = calculatearea(2,4);
printf("%d",c);
getchar();
return 0;
}
edit :
: screenshot of compiler error
From the above code we can see that you have declared the function int calculatearea(int a , int b); but have not written any definition for the same. and you are calling this function in the main. compiler is not finding the definition for the function calculatearea and giving error.
To solve this:
1) Write the definition for function calculatearea in the same file.
2) Make use of extern specifier with this function declaration and make sure that definition is present with the link library at the time of compilation.
3) As mentioned in the picture if the area.o have the definition of function calculatearea, then compile as below, this will generate a.out in linux:
gcc filename.c area.o
I am working on a school project with Nachos and I am receiving some strange error. You can see my code here: just the c file, lemme know if you need more
The output from the console looks like this:
../gnu/gcc -G 0 -c -I../userprog -I../threads -I../machine -c threadtest.c
../gnu/ld -T newscript -N start.o threadtest.o -o threadtest.coff
threadtest.o: In function `CreateClerk':
threadtest.c:804: undefined reference to `memcpy'
threadtest.o: In function `ApplicationClerkNoCustomerAtRegister':
threadtest.c:902: undefined reference to `memcpy'
threadtest.c:902: undefined reference to `memcpy'
threadtest.c:902: undefined reference to `memcpy'
threadtest.o: In function `PictureClerkNoCustomerAtRegister':
threadtest.c:954: undefined reference to `memcpy'
threadtest.o:threadtest.c:954: more undefined references to `memcpy' follow
gmake: *** [threadtest] Error 1
In my entire Nachos project folder there isn't one single call to 'memcpy' (I did a find in files search with Brackets).
Here is line 804+:
struct Clerk CreateClerk(char* _name, int _number, struct PassportOffice* _theOffice, struct Line* theLine) {
struct Clerk theClerk;
theClerk.name = _name;
theClerk.number = _number;
theClerk.theOffice = _theOffice;
theClerk.myLine = theLine;
LineSetClerk(theClerk.myLine,&theClerk);
theClerk.dataLock = CreateLock("dataLock",8);
theClerk.myBribeCV = CreateCV("myBribeCV",9);
theClerk.breakCV = CreateCV("breakCV",7);
theClerk.breakLock = CreateLock("breakLock",9);
theClerk.walkUpCV = CreateCV("walkUpCV",8);
return theClerk;
}
You should use gcc to link, not ld.
When you link via gcc it supplies the system libraries to ld . If you link via ld you have to specify all that yourself, and a pile of other things.
I want write simple C program to set ACL to one particular file on Linux. My starting code is trying to use function from "acl.h". I'm using Ubuntu 13.10, I've installed "Access control list utilities" - "acl 2.2.52-1". Here is my code:
#include <sys/acl.h>
#include <stdio.h>
int main () {
acl_t aclvar;
int count = 1;
aclvar = acl_init(count);
return 0;
}
The problem is, that I get error while compiling with "gcc myAcl.c" or "gcc -lacl myAcl.c":
/tmp/cc5sVzSR.o: In function `main':
myAcl.c:(.text+0x15): undefined reference to `acl_init'
collect2: error: ld returned 1 exit status
How can I resolve this error?
The libraries you link to needs to come last
gcc myAcl.c -lacl
For some reason this line of code is giving me quite a problem.
struct socketaddr_in clientaddr;
The error message is:
tiny.c:23:24: error: storage size of ‘clientaddr’ isn’t known
If I remove that line of code I get the following error message:
s2s2#s2s2-ThinkPad-T61:~/Documents/Cprogramming/web_server$ make
gcc -std=gnu99 -O2 -lpthread -lrt -o server tiny.c csapp.c
/tmp/ccVxw07i.o: In function `Pthread_create':
csapp.c:(.text+0x7e5): undefined reference to `pthread_create'
/tmp/ccVxw07i.o: In function `Pthread_cancel':
csapp.c:(.text+0x805): undefined reference to `pthread_cancel'
/tmp/ccVxw07i.o: In function `Pthread_join':
csapp.c:(.text+0x825): undefined reference to `pthread_join'
/tmp/ccVxw07i.o: In function `Pthread_detach':
csapp.c:(.text+0x845): undefined reference to `pthread_detach'
/tmp/ccVxw07i.o: In function `Sem_init':
csapp.c:(.text+0x895): undefined reference to `sem_init'
/tmp/ccVxw07i.o: In function `P':
csapp.c:(.text+0x8b5): undefined reference to `sem_wait'
/tmp/ccVxw07i.o: In function `V':
csapp.c:(.text+0x8d5): undefined reference to `sem_post'
/tmp/ccVxw07i.o: In function `Pthread_once':
csapp.c:(.text+0x881): undefined reference to `pthread_once'
collect2: ld returned 1 exit status
make: *** [webServer-gcc] Error 1
Here are links to the csapp.c and csapp.h files.
Thanks for all the help.
tiny.c:23:24: error: storage size of ‘clientaddr’ isn’t known
There's a reason you need to declare structs as struct structname instancename in C - that's so that the C compiler knows how much memory to allocate - and possibly how to align that data, etc.
This is the C compiler's way of telling you no such struct socketaddr_in exists - it'd be sockaddr_in.
A common way to work around the naming of structs in this way is to define them like this:
typedef struct _struct_name
{
/* ... */
} structname;
then structname can be used as a type without the struct qualifier. You don't have to do this on the definition of the struct, either, you could do it later.
So, the short answer is socketaddr_in doesn't exist as a struct in POSIX - it's sockaddr_in.