why can't I use #include "getline.c" or strindex.c in vc 6.0?
-----test30src.c------
#include <stdio.h>
#include "getline.c"
#include "strindex.c"
#define MAXLINE 1000
char pattern[] = "ould";
int main()
{
char line[MAXLINE];
int found = 0;
while(getline(line, MAXLINE) > 0)
if(strindex(line, pattern) >= 0){
printf("%s", line);
found++;
}
return found;
}
------getline.c------
#include <stdio.h>
int getline(char s[], int lim)
{
int c, i;
i = 0;
while(--lim > 0 && (c = getchar()) != EOF && c != '\n')
s[i++] = c;
if(c=='\n')
s[i++] = c;
s[i] = '\0';
return i;
}
-----strindex.c-----
int strindex(char s[], char t[])
{
int i, j, k;
for(i = 0; s[i] != '\0'; i++){
for(j = i, k = 0; s[j] == t[k]; j++, k++)
;
if(k > 0 && t[k] == '\0')
return j;
}
return -1;
}
Error:
--------------------Configuration: test30 - Win32 Debug--------------------
Linking...
getline.obj : error LNK2005: _getline already defined in test30src.obj
strindex.obj : error LNK2005: _strindex already defined in test30src.obj
Debug/test30.exe : fatal error LNK1169: one or more multiply defined symbols found
You should include string.h instead of getline.c
You must not include files which contain definitions, or you will end up with multiple instances of the same functions. Create header file which contains only declarations.
getline.c (definitions)
#include "getline.h"
int getline(char s[], int lim) { ... }
getline.h (only declarations)
#ifndef GETLINE_H
#define GETLINE_H
int getline(char s[], int lim);
#endif
main.c (include only header)
#include "getline.h"
Most IDEs will compile all .c files by default. Here, it will compile once getline.c
and strindex.c by themselves, and a second time when it compiles testsrc30.c, which include the two other files. Remember that the #include directive simply copy the contents of the included file.
At the time of linking, some symbols are found twice, and the linker can't handle the ambiguity-
The standard way of using #include's is with header (.h) files containing the declarations for your functions.
Example
//getline.h
#ifndef GETLINE_H //Header guard - avoid multiple inclusion
#define GETLINE_H
#include <stdio.h>
int getline(char s[], int lim); //function declaration
#endif // GETLINE_H
.
//getline.c
#include "getline.h"
int getline(char s[], int lim) //declaration
{
// Implement whatever your function does
}
.
// test30src.c
#include "getline.h"
int main(void)
{
// Put your code here
}
In some cases, it might be tolerable to include .c files. But in this case, you should make sure that these .c files are not compiled by themselves and linked. See this related question : Including one C source file in another?
you can include c files like this but make sure to put those files in the same directory or else give full path.
#include "complete_path/getline.c"
this will work
Related
I am trying to run the following code:
Main C file:
#include <stdio.h>
#include "my_functions.c"
#include "my_functions.h"
int main(int argv, char ** argc) {
for (int i = 0; i < argv; i++)
printf("%s\n", argc[i]);
printf("%d\n", str_find("=","-h=123"));
printf("%d\n",str_find("xyx", "aaaa"));
return 0;
}
Included .c file for Main .c file from local Library / my_functions.c file
#include <stdio.h>
#include "my_functions.h"
int str_length(char* mystring){
int i=0;
while(*mystring!='\0'){
i++;
mystring++;
}
return i;
}
Included .h file for Main .c file from local Library / my_functions.h file
#ifndef MyFunctions_h
#define MyFunctions_h
#include <stdio.h>
void str_copy(char *destination, char *source){
while(*source != '\0'){
*destination = *source;
source++;
destination++;
}
*destination = '\0';
}
#endif
int str_find(char* needle, char* haystack) {
int i = 0;
int c = 0;
while(*needle!='\0'){
++i;
++needle;
}
needle -= i;
while(*haystack!='\0'){
++c;
++haystack;
}
haystack -= c;
int k=0;
int sp = 0;
for(int d=0; d<=c; ++d, ++haystack)
{
if(*haystack==*needle && k==0)
{
sp = d;
++k;
++needle;
}else if(*haystack!=*needle && k != 0)
{
needle -= k;
k = 0;
}
}
if(sp==0){
return -1;
}else{
return sp;
}
}
Here is the error I am receiving in the Output in Visual Studio Code, which comes from the imported my_functions.h file and the int str_find(char* needle, char* haystack) function :
redefinition of 'int str_find(char*, char*)'
I am unable to resolve this issue on my own and need help.
What needs to be done to solve this issue, so my code can run properly?
The problem is that you did not guard declaration of the str_find.
main.c includes my_functions.c that includes my_functions.h and therefore declares str_find.
After that, you include my_functions.h that declares str_find again.
To fix that just move #endif to the bottom of my_functions.h
Reading Kernigan, and the given code doesn't seem to work because of 'conflicting types' error for the user function.
The compiler I'm using is cc 7.4.0. I've declared the function in the beginning and have been checking and re-checking the types seemingly forever. Guess I'm missing something.
#include <stdio.h>
#define MAXLINE 1000
int getline(char line[], int maxline);
main()
{
int len;
char line[MAXLINE];
while((len = getline(line, MAXLINE)) > 0)
;
return 0;
}
int getline(char s[], int lim)
{
int i, c;
for (i = 0; i < (lim-1) && (c = getchar())!=EOF && c != '\n'; ++i)
s[i] = c;
if (c == '\n') {
s[i] = c;
++i;
}
s[i] = '\0';
return i;
}
The error I'm getting is
func.c:4:5: error: conflicting types for ‘getline’
int getline(char line[], int maxline);
^~~~~~~
In file included from func.c:1:0:
/usr/include/stdio.h:616:20: note: previous declaration of ‘getline’ was here
extern _IO_ssize_t getline (char **__restrict __lineptr,
^~~~~~~
^~~~
func.c:25:5: error: conflicting types for ‘getline’
int getline(char s[], int lim)
^~~~~~~
In file included from func.c:1:0:
/usr/include/stdio.h:616:20: note: previous declaration of ‘getline’ was here
extern _IO_ssize_t getline (char **__restrict __lineptr,
getline is already declared (in error actually) in <stdio.h> and its signature differs from yours.
The simplest thing to do is to rename your version to something different.
You need to block the compiler from spewing non-standard crap into standard headers. You are using some compiler which has defined a non-standard getline function inside the standard library header stdio.h, which is non-conforming.
With gcc you should compile as for example gcc -std=c17 -pedantic-errors to prevent this from happening.
I cannot compile a C file that contain a function-call of a function, which is in another file. The compilation gives an error which says that there is an undefined reference even if I included the relative path to the Header file in the compilated file.
#include <stdio.h>
#include "../libft.h"
void *ft_memmove(void *dest, const void *src, size_t n)
{
unsigned char *d;
unsigned char *s;
size_t i;
d = (unsigned char *)dest;
s = (unsigned char *)src;
i = 0;
if (s < d)
{
while (n--)
d[n] = s[n];
}
else
ft_memcpy(d, s, n);
return (d);
}
int main()
{
char str[] = "memmove can be very useful.....";
ft_memmove (str+20, str+15, 11);
puts (str);
return (0);
}
The error that I get : gcc complier error
The header file : the header file
Can you help me please to resolve this problem ?
I'm studying C with K&R book. There is an exercise, here it is: "Write a program to print all input lines that are longer than 80 characters". So I wrote this code:
#include <stdio.h>
#include <stdlib.h>
int getline(char s[], int lim);
#define MINLINE 80
#define MAXLINE 1000
/*
*
*/
int main(int argc, char** argv) {
int len; //current line length
char line[MAXLINE]; //current input line
int proceed=0;
while((len=getline(line, MAXLINE))>0)
if(line[len-1]!='\n'){
printf("%s", line);
proceed=1;}
else if(proceed==1){
printf("%s", line);
proceed=0;}
else if(len>MINLINE){
printf("%s", line);
}
return 0;
}
int getline(char s[], int lim){
int i, c;
for(i=0; i<lim-1 && (c=getline())!='*' && c!='\n'; i++){
s[i]=c;
}
if(c=='\n'){
if(i<=lim-1){
s[i]=c;}
i++;}
s[i]='\0';
return i;
}
I can't compile it and I have no idea how to fix it. Could you help me?
This is the error message:
main.c:11:5: error: conflicting types for ‘getline’
In file included from /usr/include/stdio.h:62:0,
from main.c:8:
/usr/include/sys/stdio.h:37:9: note: previous declaration of ‘getline’ was here
main.c:38:5: error: conflicting types for ‘getline’
In file included from /usr/include/stdio.h:62:0,
from main.c:8:
/usr/include/sys/stdio.h:37:9: note: previous declaration of ‘getline’ was here
main.c: In function ‘getline’:
main.c:40:5: error: too few arguments to function ‘getline’
main.c:38:5: note: declared here
make[2]: *** [build/Debug/Cygwin_4.x_1-Windows/main.o] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
getline() function is already declared in stdio.h header file.if you want to redefine it in your file. just modify as my_getline()
In this for loop you need to use getchar() not getline()
for(i=0; i<lim-1 && (c=getline())!='*' && c!='\n'; i++)
for(i=0; i<lim-1 && (c=getchar())!='*' && c!='\n'; i++)
You need to use pointer in your function to get the input into line.other wise s is become local to the function.
int my_getline(char *, int); //declaration
int my_getline(char *s, int lim) //defination
{
//....
}
function call is same
len= my_getline(line, MAXLINE)
Finally use some conditional mechanism to get out of while loop in the main.
The function getline is already declared in stdio.h. Rename your function to something else.
getline is a function of the C standard library defined in stdio.h. The compiler want to use that version instead of yours.
Rename your function, for instance into my_getline and you should be fine.
Change the function name, getline exists already
I am very beginner to work with dll and linking various file.
I just know write main() function and all other in same .c file and run it.
I have one program which works for pattern matching. It takes the string and check whether it exist in entire text string or not. like
Text string: my name is john
string to be matched: name
Answer: Yes
main function is like this:
int main(int argc, const char *argv[])
{
char target[200];
char *ch = target;
char pattern[20];
int i,k,count,l;
printf("\nEnter the string: \n");
fgets(target,100,stdin);
printf("Enter the string to be matched: \n");
fgets(pattern,20,stdin);
l=strlen(pattern);
i = kmp(target, strlen(target)-1, pattern, strlen(pattern)-1);
//printf("I is : %d\n",i);
if (i == -1)
puts("False");
else
puts("True");
getch();
return 0;
}
Which calls function kmp() and get result back. We can also print the result in kmp() function. kmp() function is as follow:
int kmp(char *target, int tsize, char *pattern, int psize)
{
int i;
int *pi = compute_prefix_function(pattern, psize);
int k = -1;
if (!pi)
return -1;
for (i = 0; i < tsize; i++) {
while (k > -1 && pattern[k+1] != target[i])
k = pi[k];
if (target[i] == pattern[k+1])
k++;
if (k == psize - 1) {
free(pi);
return i-k;
}
}
free(pi);
return -1;
}
In kmp we call compute_prefix_function(pattern, psize); which is as below:
int *compute_prefix_function(char *pattern, int psize)
{
int k = -1;
int i = 1;
int *pi = malloc(sizeof(int)*psize);
if (!pi)
return NULL;
pi[0] = k;
for (i = 1; i < psize; i++) {
while (k > -1 && pattern[k+1] != pattern[i])
k = pi[k];
if (pattern[i] == pattern[k+1])
k++;
pi[i] = k;
}
return pi;
}
Header files need to be called:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
What I want to do is:
Creating an implementations in a dll/shared library format. essentially, the dll should have a function extension which take a string and return a bool saying if the string exists or not.
For that which function I need to put in .c file and header file and how to create .dll file for this?
I am using windows 7, VS 2010 and C programming.
Please explain me step by step.
I'll say more about the DLL further down, but for a start, here is the layout of the source files you'll need to do that.
You'll need three files:
main.c
kmp.h
kmp.c.
Code structure:
File main.c
#include <stdio.h>
#include "kmp.h" // this will make the kmp() function known to main()
int main(int argc, const char *argv[])
{
char target[200];
... same code as you aready have
}
File kmp.h
// prototype to make kmp() function known to external programs (via #include)
extern int kmp(char *target, int tsize, char *pattern, int psize);
File kmp.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// declare kmp prototype as DLL-export
_declspec(dllexport) int kmp(char *target, int tsize, char *pattern, int psize);
// prototype for internal helper function
static int *compute_prefix_function(char *pattern, int psize);
//
// implementation of kmp() function (and helper)
//
int kmp(char *target, int tsize, char *pattern, int psize)
{
int i;
... same program code as you aready have
}
int *compute_prefix_function(char *pattern, int psize)
{
int k = -1;
... same program code as you aready have
}
.
Now, for a first step, you can make these three files, and compile them in your current project (i.e. split your current project source into those three files, just leave out the line in kmp where it says __declspec(dllexport) and compile as before (non-DLL) to see if all works).
.
You will then need to create a DLL project for kmp.h and kmp.c (that will compile a KMP.DLL and KMP.LIB). Then you create a normal program (like your current sample) with main.c and need to link it with KMP.LIB / KMP.DLL
The following may be a bit fuzzy, because I only have VS2005 here, but the steps to create the DLL project should be essentially somewhat like this:
new project: Type Win32 / Win32-Project
name KMP
in the wizard choose Type DLL and check "Empty Project"
add your kmp.c and kmp.h files
In your main project (the one with the main.c program), you can then do
File Menu > Add > Existing Project > KMP.vcproj
This will automatically build and link the DLL from with your main.c program project.