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
I want to pass directory of Makefile to a function.
For example:
Makefile
$(DIR) = makefile directory
program
int main(int argc,char argv[])
char directory = argv[1]
How can I do that?
EDIT
Clarificaion. I want my app to work outside of the directory that i compiled it in.
Passing a symbol at compile time is done with the -D option
(example is C++ but you can transpose to C easily). You can either only define the symbol, or give it a value (which is what you want here).
DIR=$(shell pwd)
mytarget:
#echo "DIRECTORY=$(DIR)"
$(CXX) mysource.cpp -D"DIRECTORY=$(DIR)" -o mysource
Then, in your source file, the symbol DIRECTORY will hold the path. To check this, you need an additional trick (known as "double expansion"):
#define STR1(x) #x
#define STR(x) STR1(x)
#include <iostream>
int main()
{
std::cout << "directory is " << STR(DIRECTORY) << "\n";
}
You can check this similar question, and see here about the D flag.
In case the directory you need to pass is static (i.e. always the same) you can use the -D C compiler option:
cc -DDIRECTORY=/home/user/...
In your C source code you can then use DIRECTORY.
Related
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 4 years ago.
Improve this question
I am trying to read all the content of a .c file and print out in another .o file all the source code of the first file, replacing all the #include <....> in this way:
Input: #include<string.h>
Ouput: "string.h"
I have to work in pure C, without the chance to use any C++ libraries
Can someone please help me with this issue?
A simple sed on your file maybe?
sed 's/.*#include *\(<|"\)\(.*\)\(>|"\).*/"\2"/' < input.c > output.o
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 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
Using Vim as an editor, I wrote the following simple code in C and saved it as helloworld.c :
#include <stdio.h>
int main(void)
{
printf("Hello world!\n");
}
In command prompt, I wrote:
start chrome helloworld.c
This caused my browser to open up the file, but it did not print Hello World. Instead, it just displayed the code I had written. Did I not save it as a C file?
Also, I was wondering how to display the result of my C program inline on command prompt, as I am fairly new to it. While searching the internet, I could not find any answers. Am I supposed to do so from Vim? I learned that you are supposed to do ./ in the gedit command box to display the result inline, but this does not work for the one that comes with Windows.
Please help and thank you for taking the time to read and answer.
As #Ernest Friedman-Hill has already said, you normally have to compile the program. However, there are alternatives.
One alternative is the Tiny C Compiler, from http://bellard.org/tcc/. TCC does allow you to run the program without compiling it.
tcc -run helloworld.c
Does exactly what you want.
The Tiny C Compiler is not the only way to run C code from source without compiling it first. There are a few other alternatives.
CSL: http://csl.sourceforge.net/csl.html
Ch: https://www.softintegration.com/
PicoC: https://code.google.com/p/picoc/
CINT: http://root.cern.ch/drupal/content/cint
I hope this helps.
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.
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