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
Related
I'm a beginner in Coding. I'm trying to write a code in C. Uptill this code every code was running smoothly. But after writing the following code the Visual Studio Code is giving errors. The most repeated was collect2.exe: error: ld returned 1 exit status. I saved the code before running. I tried reinstalling the gcc MinGW compiler and Visual Studio Code IDE but nothing happened. I also tried the Geany IDE but it is showing the same error. What should I do?
#include<stdio.h>
#include<stdlib.h>
int mian(){
int marks[4];
marks[0]=34;
printf("Marks of Student 4 is %d",marks[0]);
return 0;
}
PS D:\Codes> cd "d:\Codes\CPrograms\" ; if ($?) { gcc arrays.c -o arrays } ; if ($?) { .\arrays }
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../libmingw32.a(main.o):(.text.startup+0xa0): undefined reference to `WinMain#16'
collect2.exe: error: ld returned 1 exit status
Error: Id returned 1 exit status (undefined reference to 'main')
This error is occurred on following cases,
If main() is not written in lowercase, like you used Main(), MAIN(), mAin() or anything else.
If main() does not exist in the program or by mistake you mistyped the main().
In your case you mistyped the main()
change mian()--> main()
#include<stdio.h>
#include<stdlib.h>
int main(){
int marks[4];
marks[0]=34;
printf("Marks of Student 4 is %d",marks[0]);
return 0;
}
I am trying to compile a C file containing this code :
#include <stdio.h>
#include <stdlib.h>
#include <curl/curl.h>
int main()
{
curl_global_init( CURL_GLOBAL_ALL );
CURL * myHandle;
CURLcode result; // We’ll store the result of CURL’s webpage retrieval, for simple error checking.
myHandle = curl_easy_init ( ) ;
// Notice the lack of major error checking, for brevity
curl_easy_setopt(myHandle, CURLOPT_URL, "http://www.example.com");
result = curl_easy_perform( myHandle );
curl_easy_cleanup( myHandle );
printf("LibCurl rules!\n");
return 0;
}
,
Well when I try to compile :
gcc url.c -lcurl
I get the following errors:
F:\MinGW\home>gcc url.c -lcurl
C:\Users\Ehsan\AppData\Local\Temp\ccF7doFN.o:url.c:(.text+0x8e): undefined reference to `_imp__curl_global_init'
C:\Users\Ehsan\AppData\Local\Temp\ccF7doFN.o:url.c:(.text+0x95): undefined reference to `_imp__curl_easy_init'
C:\Users\Ehsan\AppData\Local\Temp\ccF7doFN.o:url.c:(.text+0xbf): undefined reference to `_imp__curl_easy_setopt'
C:\Users\Ehsan\AppData\Local\Temp\ccF7doFN.o:url.c:(.text+0xcd): undefined reference to `_imp__curl_easy_perform'
C:\Users\Ehsan\AppData\Local\Temp\ccF7doFN.o:url.c:(.text+0xdf): undefined reference to `_imp__curl_easy_cleanup'
collect2.exe: error: ld returned 1 exit status
I downloaded this distribution
and copied all files to bin, include , and lib folder.
What am I missing?
Update
Well I fixed the problem by using -lcurldll.
In my lib folder I have both libcurl.a and libcurldll.a.
Why linking with libcurl.a can't compile but with libcurldll.a it works fine?
You can check the lib name by:
readelf -d [LIB_PATH]
That would solve you the problem.
I'm trying to set a function pointer to point to the pow function.
Here's my code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void){
double (*func)(double, double) = pow;
return 0;
}
But the program doesn't compile.
I get this error:
$ gcc test.c -o test
/tmp/ccD6Pmmn.o: In function `main':
test.c:(.text+0x8): undefined reference to `pow'
collect2: error: ld returned 1 exit status
I'm using Ubuntu 15.10.
Anyone knows what's wrong with my code?
Thanks
You need to compile with -lm via command line or configure your IDE to add it into the linking process. This is due to the fact that some libraries are pretty large and to avoid taking up space in your program and compilation time, this was setup at the beginning of C when computers were much slower and would take MUCH more to compile and a matter of space was CRUCIAL.
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.
When I compile this code, I get the following gcc errors:
/tmp/ccUigsI6.o: In function `main':
/home/matt/Dropbox/school/2011/cs3210/test/sizeterm.c:9: undefined reference to `setupterm'
/home/matt/Dropbox/school/2011/cs3210/test/sizeterm.c:10: undefined reference to `tigetnum'
/home/matt/Dropbox/school/2011/cs3210/test/sizeterm.c:11: undefined reference to `tigetnum'
collect2: ld returned 1 exit status
make: *** [sizeterm] Error 1
Here's the code:
#include <stdio.h>
#include <term.h>
#include <curses.h>
#include <stdlib.h>
int main()
{
int nrows, ncolumns;
setupterm(NULL, fileno(stdout), (int *)0);
nrows = tigetnum("lines");
ncolumns = tigetnum("cols");
printf("This terminal has %d columns and %d rows\n", ncolumns, nrows);
exit(0);
}
Libncurses is installed correctly on my machine. I get the same results from my Arch linux laptop, and the Ubuntu server installed at my school. This particular piece of code is taken directly out of the book. Am I doing something wrong? I've done some googling and it looks as though people have had this problem before, but I can't narrow down a solution.
You forgot to actually link against ncurses. Add -lcurses to the gcc command line.
This is exactly what you find in the same book as where you found this code:
$ cc -o badterm badterm.c -lncurses
Beginning linux programming 4th edition, chapter 5: Terminals, page 196.