In Debian 8's source code /source/procps-3.3.9/lib/fileutils.c line 38 is:
char const *write_error = _("write error");
I am confused about the _("write error") part. Google showed that result on variable naming convention or library reserved names, but nothing about when _ was on the right side of = and before a () quoted string.
I also put this line into a simplest test program as only useful line then had compilation failed saying:
test.c:5:20: warning: implicit declaration of function ‘_’ [-Wimplicit-function-declaration]
char const *str = _("test string");
^
test.c:5:20: warning: initialization makes pointer from integer without a cast [-Wint-conversion]
/tmp/cczQpqTh.o: In function `main':
test.c:(.text+0x15): undefined reference to `_'
collect2: error: ld returned 1 exit status
Does anyone know what _(" ") format means?
This is the standard way to mark up strings for translation using GNU gettext, a free software translation tool.
The _() macro is found by an external tool which extracts the text to make it translatable, as well as (at run-time) do look-ups to replace the literal with the necessary translation.
There is nothing special about the name _, it's just a very short but perfectly valid C identifier. Perhaps it's a bit iffy to begin a public symbol with an underscore, I'm not sure right now.
The error you're getting is because your test program very likely fails to include the <libintl.h> header (part of gettext, of course) which declares this macro. Thus you get the normal "undefined reference" error, as expected.
Related
Been working on my own language for some time now and I've been implementing implicit and explicit casts. Went back to check what happens when invoking them. Everything went well, until I tried to invoke a cast from a double. Passing doubles to functions for some reason results in clang giving me an error when linking the obj file to the cpp file that invokes it, namely:"clang.exe: error: linker command failed with exit code 1143 (use -v to see invocation)program.obj : fatal error LNK1143: invalid or corrupt file: no symbol for COMDAT section 0x5"
I've then noticed this also happens with floats, but does not occur with integers and the likes. I've written a short function (takes a double by value and returns it) to check whether the problem was related to parameters of types float/double. The error only occurs when invoking the function, when defining the function but not invoking it, everything functioned properly. I've also tried to simply store a double literal in memory (alloca) and that worked as well, so the problem has to be with actually passing the argument to the function.
Simple LLVM IR for a function that takes a double by value and returns it, then being invoked from main (main is names _mainCRTStartup as to not confuse it with the symbol for the main function from the cpp file that then invokes it (extern "C"))
Thanks for any help in advance! :D
For some bizarre reason, when I try to use the function get_current_dir_name with MinGW GCC compiler,
I get this result on linkage:
undefined reference to `get_current_dir_name'
collect2.exe: error: ld returned 1 exit status
But, I get this only when using the function like this
printf("%i", get_current_dir_name());
or this
printf("%s", get_current_dir_name());
When I try to do
printf(get_current_dir_name());
I get this, which makes no sense, because the function returns a char *, according to docs:
tester.c: In function 'main':
tester.c:16:2: warning: passing argument 1 of 'printf' makes pointer from integer without a cast [enabled by default]
printf(get_current_dir_name());
^
In file included from tester.c:1:0:
c:\mingw\include\stdio.h:294:37: note: expected 'const char *' but argument is of type 'int'
_CRTIMP int __cdecl __MINGW_NOTHROW printf (const char*, ...);
Google seem to really dislike talking about C, because I can find how to get the workdir on almost any existing language, except C. The only thing that pops up are some docs, which describe 3 functions: getcwd, getwd, and get_current_dir_name. I really want to use the get_current_dir_name one because of it's cleanness.
How do I deal with this? Is this a minGW bug? Or am I missing something?
You apparently failed to include any header that contains a declaration of get_current_dir_name(). Thus, the compiler will assume a return value of int, which is not a valid first argument for printf() (you should increase the warning levels so you'll get an error instead of just a warning).
Furthermore, linking fails, so you also do not link against a library that implements the function, which is expected: get_current_dir_name() is a GNU extension and not part of the C standard library.
On Windows, you need to use the equivalent functionality provided by the Windows API, ie GetCurrentDirectory(), declared in windows.h.
if(find_task_by_vpid(pid))
{
myProcess = kmalloc(sizeof(find_task_by_vpid(pid)), GFP_KERNEL);
myProcess = find_task_by_vpid(pid);
}
I've included sched.h where this method is declared, but am getting the following errors:
(on the first line) warning: implicit declaration of function 'find_task_by_vpid'
(on the last line) warning:assignment makes pointer from integer without a cast
Presumably the issues are related, but I can't figure out what the matter is.
Running Ubuntu (6.06.2) linux kernel version 2.6.15.51-server.
I believe you're including the wrong sched.h. My system has several sched headers but only /usr/src/linux-headers-3.5.0-18/include/linux/sched.h contains the declaration of find_task_by-vpid(). I recommend you check your include paths.
I am a little puzzled. I have project that I compile with
CFLAGS=-g -O2 -Wall -Wextra -Isrc/main -pthread -rdynamic -DNDEBUG $(OPTFLAGS) -D_FILE_OFFSET_BITS=64 -D_XOPEN_SOURCE=700
Now I want to use mkdtemp and therefor include unistd.h
char *path = mkdtemp(strdup("/tmp/test-XXXXXX"));
On MacOSX the compilation gives some warnings
warning: implicit declaration of function ‘mkdtemp’
warning: initialization makes pointer from integer without a cast
but compiles through. While mkdtemp does return a non-NULL path accessing it results in a EXC_BAD_ACCESS.
Question 1: The template is strdup()ed and the result is non-NULL. How on earth can this result in an EXC_BAD_ACCESS?
Now further down the rabbit hole. Let's get rid of the warnings. Checking unistd.h I find the declaration hidden by the pre processor.
#if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
...
char *mkdtemp(char *);
...
#endif
Adding -D_DARWIN_C_SOURCE to the build makes all the problems go away but leaves me with a platform specific build. The 10.6 man page just says
Standard C Library (libc, -lc)
#include <unistd.h>
Removing the _XOPEN_SOURCE from the build makes is work on OSX but then it fails to compile under Linux with
warning: ‘struct FTW’ declared inside parameter list
warning: its scope is only this definition or declaration, which is probably not what you want
In function ‘tmp_remove’:
warning: implicit declaration of function ‘nftw’
error: ‘FTW_DEPTH’ undeclared (first use in this function)
error: (Each undeclared identifier is reported only once
error: for each function it appears in.)
error: ‘FTW_PHYS’ undeclared (first use in this function)
Question 2: So how would you fix this?
The only fix I have found is to #undef _POSIX_C_SOURCE right before the unistd.h include ...but that feels like an ugly hack.
You've asked two questions here, and I'm just going to answer the first:
Question 1: The template is strdup()ed and the result is non-NULL. How on earth can this result in an EXC_BAD_ACCESS?
As the warnings above tell you:
warning: implicit declaration of function ‘mkdtemp’
This means it couldn't find the declaration for mkdtemp. By C rules, that's allowed, but it's assuming the function returns an int.
warning: initialization makes pointer from integer without a cast
You've told the compiler "I've got a function that returns int, and I want to store the value in a char*". It's warning you that this is a bad idea. You can still do it, and therefore it compiles.
But think about what happens at runtime. The actual code you link to returns a 64-bit char*. Then your code treats that as a 32-bit int that it has to cast to a 64-bit char*. How likely is that to work?
This is why you don't ignore warnings.
And now for the second question:
Question 2: So how would you fix this?
Your problem is that you're explicitly passing -D_XOPEN_SOURCE=700, but you're using a function, mkdtemp, that isn't defined in the standard you're demanding. That means your code shouldn't work. The fact that it does work on linux doesn't mean your code is correct or portable, just that you happened to get lucky on one platform.
So, there are two rather obvious ways to fix this:
If you want to use _XOPEN_SOURCE=700, rewrite your code to only use functions that are in that standard.
If you've only added _XOPEN_SOURCE=700 as a hack that you don't really understand because it seemed to fix some other problem on linux, find the right way to fix that problem on linux.
It may turn out that there's a bug on one platform or another so there just is no right way to fix it. Or, more likely, you're using a combination of non-standard functions that can be squeezed in on different platforms with a different set of flags on each. In that case, your Makefile (or whatever drives the build) will have to pass different flags to the compiler on different platforms. This is pretty typical for cross-platform projects; just be glad you only have one flag to worry about, and aren't building 3000 lines worth of autoconf.
I'm trying to convert an integer to a character to write to a file, using this line:
fputc(itoa(size, tempBuffer, 10), saveFile);
and I receive this warning and message:
warning: implicit declaration of 'itoa'
undefined reference to '_itoa'
I've already included stdlib.h, and am compiling with:
gcc -Wall -pedantic -ansi
Any help would be appreciated, thank you.
itoa is not part of the standard. I suspect either -ansi is preventing you from using it, or it's not available at all.
I would suggest using sprintf()
If you go with the c99 standard, you can use snprintf() which is of course safer.
char buffer[12];
int i = 20;
snprintf(buffer, 12,"%d",i);
This here tells you that during the compilation phase itoa is unknown:
warning: implicit declaration of
'itoa'
so if this function is present on your system you are missing a header file that declares it. The compiler then supposes that it is a function that takes an unspecific number of arguments and returns an int.
This message from the loader phase
undefined reference to '_itoa'
explains that also the loader doesn't find such a function in any of the libraries he knows of.
So you should perhaps follow Brian's advice to replace itoa by a standard function.