Reputation: 11
I was trying to run the following program when this big error occurred. What am I doing wrong?
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main() {
char *file = "zahl.txt";
int fd;
struct flock lock = { F_WRLCK, SEEK_SET, 0, 0, 0 };
lock.l_pid = getpid();
fd = open(file, O_RDWR | O_APPEND);
if (fcntl(fd, F_SETLKW, &lock) != -1)
//setting the lock to wait while another instance is already running
{
FILE *fp1;
int i;
char buff[255];
fp1 = fopen(file, "a+"); //read from file
while (fgets(buff, 255, fp1) != NULL); //moves to last line of file
i = atoi(buff);
i++;
char temp[10];
sprintf(temp, "%d\n", i); //printing the incremented value to temp
fputs(temp, fp1);
fclose(fp1);
lock.l_type = F_UNLCK;
fcntl(fd, F_SETLK, &lock);
close(fd);
}
return 0;
}
[rmig0489@linux Assign9]$ gcc -Wall -o -g ink ink.c
ink: In function
_start': (.text+0x0): multiple definition of
_start' /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../lib64/crt1.o:(.text+0x0): first defined here ink: In function_fini': (.fini+0x0): multiple definition of
_fini' /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../lib64/crti.o:(.fini+0x0): first defined here ink:(.rodata+0x0): multiple definition of_IO_stdin_used' /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../lib64/crt1.o:(.rodata.cst4+0x0): first defined here ink: In function
__data_start': (.data+0x0): multiple definition of__data_start' /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../lib64/crt1.o:(.data+0x0): first defined here ink:(.rodata+0x8): multiple definition of
__dso_handle' /usr/lib/gcc/x86_64-redhat-linux/4.4.7/crtbegin.o:(.rodata+0x0): first defined here ink: In function_init': (.init+0x0): multiple definition of
_init' /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../lib64/crti.o:(.init+0x0): first defined here /tmp/ccmuhndv.o: In functionmain': ink.c:(.text+0x0): multiple definition of
main' ink:/home/scs/licenta/an1/gr712/rmig0489/C-Programm/Assign9/ink.c:6: first defined here /usr/lib/gcc/x86_64-redhat-linux/4.4.7/crtend.o:(.dtors+0x0): multiple definition of `DTOR_END' ink:(.dtors+0x8): first defined here /usr/bin/ld: warning: Cannot create .eh_frame_hdr section, --eh-frame-hdr ignored. /usr/bin/ld: error in ink(.eh_frame); no .eh_frame_hdr table will be created. collect2: ld returned 1 exit status
Upvotes: 0
Views: 365
Reputation: 144530
The problem is in the command line: gcc -Wall -o -g ink ink.c
The -o
argument must be followed by the name of the output file, so -g
is used for that, and gcc
sees ink
(the executable produced by a previous command) as a binary module to link along with the object module compiled from ink.c
and the C library, hence all the duplicate internal symbols.
Use this command:
gcc -Wall -g -o ink ink.c
Note also these problems in your code:
relying on the contents of buff
after the last call to fgets()
is incorrect, especially if the file happens to be empty. You should instead convert each and every line successfully read and use the last conversion.
you must call fseek(fp1, 0L, SEEK_CUR)
before switching from reading the file to writing to it.
char temp[10];
is not large enough to convert all possible int
values. You should make the array larger and use snprintf
for safety.
Upvotes: 1