Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have created a a simple file under one work area, build it and executed successfully. And again created another file under same work area. I am getting linking error like:
"====Linking===== Error: Duplicate public_main in module...." while building and I am getting the same error when I try to execute first file also.
Please see the below code and suggest.
File1: n_numbers.c
#define N 10
main()
{
int count;
float sum, number;
count=0;
sum=0;
printf("Enter 10 numbers to calculate the Sum");
while(N<10)
{
scanf("%f", &number);
sum=sum+number;
count=count+1;
}
printf("sum=5.2%", sum);
}
File2: two.numbers.c
main()
{
int a,b,c;
printf("Enter Two numbers for a and b\n");
scanf("%d %d",&a, &b);
c=a+b;
printf("c=%d", c);
}
I suspect your problem is:
gcc file_a.c
a.out
Works
gcc file_b.c
a.out
Works
gcc file_a.c file_b.c
linker error
Will be because gcc is trying to make a single executable and each executable must have exactly one main function.
If file_a and file_b both define a function called public_main the problem becomes which one is intended in any given invocation - C solves this by a simple rule - you can only have one of each function in each executable.
N.B. Other languages have different rules, C++ can have multiple functions with the same name but different prototypes, (overloading), or within different classes, (namespaces), it actually does both via what is called name mangling. Python uses namespaces and sophisticated scoping rules, etc.
Copied from comments!
Hi Steve, I had only one main() in each executable, but still i am
getting same error – user2714972
You have 2 .c files and you are linking them into a single executable,
(.exe), that is what gcc does when you give it more than one .c file
if you would like to make 2 executables you need to call gcc twice,
once with each .c and with -o differcent_exe_name to stop both being
called a.out or a.exe – Steve Barnes
If, as some have suggested, you are using an IDE you need to either create separate projects or specify separate targets depending on the IDE.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 1 year ago.
Improve this question
I wanted running a c program through the terminal, and instead of writing the following: gcc -o main.c program.c
,
I have written the following: gcc -o main,c program.c. This unfortunately disappeared my file "program.c", perhaps deleted it, and created a new txt file called "main,c" which looks like that:
Now, I have searched a lot this issue over a few sites and I couldn't find a compatible case for this one. Is there a way to recover the c file?
In addition, all remained is program.c.o file and main,c file.
Thanks!
Recovering the original source file is not a programming question, it is a general computing issue. With any tool (word-processor, photo-editor or as in this case a compiler), if you rewrite a file, the original is replaced and so no longer available.
You seem very confused over how to drive the compiler - the action you said you have taken would not on its own caused the results you state; I suspect in your confusion you have taken other actions that have simply worsened your situation.
Neither command line would have been correct in any case. The -o option specifies the output file name, and in the first case it would have created or overwritten main.c, and in the second it would have created main,c.
If you only executed the second command, main,c will have been created, and main.c will still exist. Neither of those commands will have deleted anything, but may have overwritten the sourcefile with object code file rendering it useless.
It is not clear what you were actually trying to do, but if you were trying to compile main.c to create program called program, then:
gcc -o program main.c
If however you were trying to compile and link two source files called main.c and program.c to create an executable, then:
gcc -o program main.c program.c
or even:
gcc -c main.c
gcc -c program.c
gcc -o main.o program.o
From the comments it seems in fact you are attempting to compile program.c to create an executable called main.c. Don't do that! The .c extension is used to indicate a C source file - input to the compiler). The output of the compiler is binary object code (and following the implicit link a binary executable). What you need then is simply:
gcc -o program program.c
None of that explains how you managed to delete program.c or even create a file called program.c.o rather then simply program.o - "the steps to reproduce" provided are not plausible. You have clearly made a string of erroneous actions to end up where you are. Unpicking it is probably unproductive; it is gone, you must start again, take care, use the compiler correctly and perhaps make a back-up copy or use a version control system before experimenting further.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm trying to build a big project step by step. I'm working on Linux and using qt-creator and gcc compiler. I include a set of related files in the .pri file and then build, test functionality and then add a new set of existing files and so on.
The project compiled successfully then I included a folder containing some header files then when compiled I got many errors in libc-header-start.h, string.h, cpu-set.h, time.h and many other stdlib and system files. Errors like missing binary operator before token (, unknown type name __cpu_mask, expected ';', ',' or ')' before '*' token.
I think these are likely to be preprocessor errors that are caused, for example, if I forget a semicolumn or so, right?
My question is, how can I locate the exact location in the header/source files where all these errors started, for example, the line of code where the semicolumn is missing.
This answer is just collecting the comments on my question.
Here is what can help locating what is the root cause of preprocessor errors:
Make sure that the names of your header files don't conflict with standard header files.
Check the first file in the errors list. Check where this file is included. If this file is included by another standard header file, check where the parent header file is included in your code. The root cause of the errors may be in the lines before that #include statement in your code. For example, you may have forgetten a semi-column before this #include.
Use gcc -H file.c to check all the header files included with file.c.
Use gcc -E file.c to generate a preprocessed version of file.c, then you will have the full end picture of your file which you can then investigate.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
i'm having a strange problem where i get undefined reference to "PDC_ungetch" while I can use without any problem other functions from curses.h, for example:
#include <curses.h>
int main(){
initscr();
int ch = getch();
ungetch(ch);
return 0;
}
With this code i only get undefined reference to "PDC_ungetch" while initscr() works with no problems, what would be the problem?
My CMake is the following:
cmake_minimum_required(VERSION 3.3)
project(rogue)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Werror")
set(SOURCE_FILES main.c gamelib.c gamelib.h maze.c maze.h) //these are other files I use
find_package(Curses REQUIRED)
include_directories(${CURSES_INCLUDE_DIR})
add_executable(rogue ${SOURCE_FILES} gamelib.c gamelib.h maze.c maze.h) //Same here
target_link_libraries(rogue ${CURSES_LIBRARIES})
Thanks in advance for the help.
If you look at the header file <curses.h> from PDCurses, that particular function is the only one treated in that way: defining a macro to use the function with a PDC_ prefix.
If you happen to be cross-compiling (from Cygwin for example), and if the cmake macros are not setup/used properly, they could find the system curses library, which does not use that naming convention. In that case, you would get just that one error when trying to link.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I want to find the specific definitions of some functions which are declared in a .h file. The question is how I can accomplish that? Because there're hundreds of .c files in the directory. And I have tried to use grep command, it doesn't work efficiently. Is there any other method?
Slice of my .h file:
248 #define EXPAND(a,b,c) a b
I use vim, so:
:tag EXPAND
E433: No tags file
E426: tag not found
248, etc. is the line number. and 433, 426 has no relation with EXPAND, one is a blank line, the other is within a comment section.
EXPAND is a macro? I thought it was a function because it appeared in other .c files like:
u[EN] = EXPAND(v[VX]*v[VX], + v[VY]*v[VY], + v[VZ]*v[VZ])
#didierc is right, I found this is a macro. I'm really green to C. Many thanks to #Basile Starynkevitch, offered a way to trace a function, that's another prob. puzzled me
Use etags with emacs (or else ctags, e.g. if using vi). Instead of grep consider using ack.
Read more about the linker (e.g. Levine's book Linkers & Loaders; details are operating system specific); on Linux see binutils. Notice that externapplies to declarations, not definitions of function or variable names.
Don't forget to run the ctags or etags command from time to time, and before using tags in your emacs or vi editor.
About macros: please understand that the C preprocessor is the first phase of compilation. Read documentation of GNU cpp. Don't expect macros to be functions, they are textual devices! Use perhaps gcc -Wall -C -E foo.c > foo.i (with some more options to gcc, e.g. -I and -D ones) to get the preprocessed form foo.i of source file foo.c; look inside the generated foo.i with your editor.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Sir pls tell me how to create .I file (extended source file) in c
A common way to create files in C is with the fopen() function.
#include <stdio.h>
FILE *handle;
handle = fopen("extended.I", "w");
if (handle != NULL) {
/* ... */
fclose(handle);
}
Terribly vague question, but it sounds like you are using Visual Studio. Right-click your project, Properties, C/C++, Preprocessor, change "Generate Preprocessed File" to Yes.
After you rebuild, you'll get the .i files with the preprocessor output in your project directory.
arsane's comment is the correct response if you are on Linux. "To expand the macro, you can try
gcc -E -o main.I main.c
"
When using the gcc compiler system, it is possible to halt the compiler system at particular phases by using compiler flags. -E for .i files, -S for .s files (an assembly language version of the program)
From the gcc man page:
-E Stop after the preprocessing stage; do not run the compiler proper.
The output is in the form of preprocessed source code, which is
sent to the standard output.
Input files which don't require preprocessing are ignored.
The following command will create a .i file from a .c file
cc -E main.c -o main.i