1

I am compiling a function and getting warning:

**/tmp/ccPFK7nG.o: warning gets is dangerous and should not be used.**

Now I know why the warning is coming, the part that I am not aware is the location from where the warning is coming keeps changing. Every time I compile the code the location is /tmp/some_file.o Is it like gcc makes the temporary object file in /tmp directory and when the executable is made it removes it from there?

glglgl
  • 89,107
  • 13
  • 149
  • 217
lokesharo
  • 305
  • 2
  • 11
  • Have you grepped for calls to `gets`? Does your project contain any pre-build static libraries or other binary modules that might call it? – user3553031 Aug 04 '14 at 06:15
  • 1
    I am using gets to get the user input and compiling a single .c file with no static libs involved. – lokesharo Aug 04 '14 at 06:18
  • 1
    TMPDIR: If TMPDIR is set, it specifies the directory to use for temporary files. GCC uses temporary files to hold the output of one stage of compilation which is to be used as input to the next stage: for example, the output of the preprocessor, which is the input to the compiler proper. (https://gcc.gnu.org/onlinedocs/gcc/Environment-Variables.html). – polslinux Aug 04 '14 at 06:19
  • @polslinux TMPDIR is not set. – lokesharo Aug 04 '14 at 06:26
  • @n.m. : using gets is wrong and I get it but I want to know the reason for the different .o files it is making. – lokesharo Aug 04 '14 at 06:27
  • @lokesharo also on Gentoo is not set but GCC compiles inside the /tmp directory! – polslinux Aug 04 '14 at 06:28
  • 1
    "Is it like gcc makes the temporary object file in /tmp directory and when the executable is made it removes it from there?" This is exactly what happens. Why is this surprising? – n. m. could be an AI Aug 04 '14 at 06:36
  • GCC is presumably using a function like http://en.cppreference.com/w/cpp/io/c/tmpfile to create a temporary file... it will work out which directory is appropriate and choose a filename, and make sure the file is deleted afterwards even if the creating process is terminated in a way that doesn't give it a chance to delete the file. – Tony Delroy Aug 04 '14 at 06:36
  • 3
    The title is misleading. The title should summarize the problem. If the warning is not the problem, it should not be mentioned in the title. – n. m. could be an AI Aug 04 '14 at 06:39
  • As was mentionned several times, thi is not at all a duplicate of that other question. The point is a completely different one. People would see that if they read the question thoroughly. – glglgl Aug 04 '14 at 10:52

3 Answers3

2

Every time I compile the code the location is /tmp/some_file.o Is it like gcc makes the temporary object file in /tmp directory and when the executable is made it removes it from there?

What you see is a side effect of the -flto option in gcc, that enables link time optimization. The compiled source for this second pass is indeed a temporary, containing precompiled data from object files.

To see the real culprit, you may need to remove this option and recompile, although the warning should appear with the correct file location in the first pass.

Turbo J
  • 7,563
  • 1
  • 23
  • 43
1

Suggest to remove the usage of gets(). Since ISO C11 removes the specification of gets() from the C language.

http://linux.die.net/man/3/gets

Read BUGS and Conforming to section.

Jeyaram
  • 9,158
  • 7
  • 41
  • 63
  • 2
    This doesn't answer the question at all. He ask why he is getting the warning in different files, not the reason behind this warning.. – Antzi Aug 04 '14 at 06:30
1

If compiling and linking at the same time, such as

gcc a.c b.c c.c -o wholeprogram

each mentioned C module gets compiled into a temporary object file, then all object files are linked together to get the final executable.

The names of these temporary obejct files are created dynamically and on the fly and, thus, change on every invocation.

glglgl
  • 89,107
  • 13
  • 149
  • 217