This question already has answers here:
Error when connecting to Postgres database in C - using libpq-fe.h
(2 answers)
Closed 9 years ago.
I have the following code:
#include <stdio.h>
#include <stdlib.h>
#include <libpq-fe.h>
int main(int argc, char* argv[])
{
//Start connection
PGconn* connection = PQconnectdb("host=webcourse.cs.nuim.ie dbname=cs621 sslmode=require user=ggales password=1234");
if (PQstatus(connection) ==CONNECTION_BAD)
{
printf("Connection error\n");
PQfinish(connection);
return -1; //Execution of the program will stop here
}
printf("Connection ok\n");
//End connection
PQfinish(connection);
printf("Disconnected\n");
return 0;
}
When I run it, I get the following error:
/tmp/cc73kO0N.o: In function `main':
main.c:(.text+0x15): undefined reference to `PQconnectdb'
main.c:(.text+0x25): undefined reference to `PQstatus'
main.c:(.text+0x40): undefined reference to `PQfinish'
main.c:(.text+0x5d): undefined reference to `PQfinish'
collect2: error: ld returned 1 exit status
This is strange, as PQconnectdb etc are all functions that are defined in libpq-fe.h, which I have already included in the code.
Any help would be great thanks.
#include <libpq-fe.h> does not link to the library, it only includes information about the functions and data types that the library provides.
You must tell the linker where the references that are declared in libpq-fe.h can actually be found.
If you are using a Makefile to compile you code you should add -lpq to your LDFLAGS or linking command.
Post the command you are running to compile to give us more information.
Related
This question already has answers here:
C error: undefined reference to function, but it IS defined
(6 answers)
Closed 9 months ago.
I have written a code that just basically add two numbers. file is the self made header file
named xoxo.h
extern int add(int r,int m);
This is my second file that contains the function defination of the function add.The name is
run.c
#include "xoxo.h"
int add (int i,int f) {
return (i+f);
}
This is my main file tester.c
#include "xoxo.h"
#include <stdio.h>
void main() {
printf("%d",add(1,2));
}
The error is shown as
PS C:\Users\HOME\Desktop\New folder> gcc tester.c
C:\Users\HOME\AppData\Local\Temp\ccBwWXFk.o:tester.c:(.text+0x1e):
undefined reference to `add' collect2.exe: error: ld returned 1 exit
status
plz help
When you compile the application, you need to provide run.c as well, otherwise the application cannot find the implementation of add.
Run gcc tester.c run.c instead.
This question already has answers here:
Ordering of object files and libraries in static linking
(1 answer)
Why does the order in which libraries are linked sometimes cause errors in GCC?
(9 answers)
Closed 3 years ago.
I try to Compile this Simple Lua Tutorial Program:
#include <stdio.h>
#include <string.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
int main (void) {
char buff[256];
int error;
lua_State *L = lua_open(); /* opens Lua */
luaopen_base(L); /* opens the basic library */
luaopen_table(L); /* opens the table library */
luaopen_io(L); /* opens the I/O library */
luaopen_string(L); /* opens the string lib. */
luaopen_math(L); /* opens the math lib. */
while (fgets(buff, sizeof(buff), stdin) != NULL) {
error = luaL_loadbuffer(L, buff, strlen(buff), "line") ||
lua_pcall(L, 0, 0, 0);
if (error) {
fprintf(stderr, "%s", lua_tostring(L, -1));
lua_pop(L, 1); /* pop error message from the stack */
}
}
lua_close(L);
return 0;
}
With the Following Command:
gcc -I/usr/include/lua50 -L/usr/lib/liblua50.a -llua50 luainterpret.c
So the Headers are Linked and Library Binary should also be Linked right?
However i get the following undefined References:
/tmp/ccA3kOUt.o: In function `main':
luainterpret.c:(.text+0x1b): undefined reference to `lua_open'
luainterpret.c:(.text+0x31): undefined reference to `luaopen_base'
luainterpret.c:(.text+0x40): undefined reference to `luaopen_table'
luainterpret.c:(.text+0x4f): undefined reference to `luaopen_io'
luainterpret.c:(.text+0x5e): undefined reference to `luaopen_string'
luainterpret.c:(.text+0x6d): undefined reference to `luaopen_math'
luainterpret.c:(.text+0xa1): undefined reference to `luaL_loadbuffer'
luainterpret.c:(.text+0xc3): undefined reference to `lua_pcall'
luainterpret.c:(.text+0xf6): undefined reference to `lua_tostring'
luainterpret.c:(.text+0x11f): undefined reference to `lua_settop'
luainterpret.c:(.text+0x152): undefined reference to `lua_close'
collect2: error: ld returned 1 exit status
I checked the /usr/lib/liblua50.a file with nm and the Functions above are indeed there! Why is gcc then not able to find said Functions?
Can someone tell me what im doing wrong?
Instead of putting the library before the source file (which makes use of the functions present in the library), try putting it afterwards, like
gcc -I/usr/include/lua50 -L/usr/lib/liblua50.a luainterpret.c -llua50
From the online gcc manual
It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, foo.o -lz bar.o searches library z after file foo.o but before bar.o. If bar.o refers to functions in z, those functions may not be loaded.
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
This question already has answers here:
Undefined reference to sqrt (or other mathematical functions)
(5 answers)
Closed 9 years ago.
#include <stdio.h>
#include <math.h>
int main()
{
printf("%.81f\n", 1+2*sqrt(3)/(5-0.1));
return 0;
}
output:
/tmp/a4-4oU730.o: In function main':
a4.c:(.text+0x4f): undefined reference tosqrt'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Try adding -lm for libm for math to your build command. That said, your code works fine for me using clang 4.1 on Mac OS.
I am working from a book: TCP/IP Sockets in C and its website code.
I am trying to build a client and server based on those files. My make gives lots of
error related to not being able to find functions from DieWithMessage.c
Here it is:
#include <stdio.h>
#include <stdlib.h>
#include "Practical.h"
void DieWithUserMessage(const char *msg, const char *detail) {
fputs(msg, stderr);
fputs(": ", stderr);
fputs(detail, stderr);
fputc('\n', stderr);
exit(1);
}
void DieWithSystemMessage(const char *msg) {
perror(msg);
exit(1);
}
When I do gcc DieWithMessage.c, I get the following error:
/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/../../../crt1.o: In function _start':
(.text+0x18): undefined reference tomain'
collect2: ld returned 1 exit status
How do I compile this by itself so that the errors will stop happening when using the makefile?
Thanks for any help.
Your C code needs a main function if you're going to try an link/run it. This is a requirement for hosted C applications under the standard.
That error message indicates what's wrong. The C runtime/startup code (CRT) has an entry point of start which sets up the environment then calls your main. Since you haven't provided a main, it complains.
If you only want to generate an object file for later linking with a main (see here for one description of the process), use something like:
gcc -c -o DieWithMessage.o DieWithMessage.c
(-c is the "compile but don't link" flag). You can then link it later with your main program with something like (although there are other options):
gcc -o myProg myProg.c DieWithMessage.o
If you want a placeholder main to update later with a real one, you can add the following to your code:
int main (void) { return 0; }