Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #107869

Re: Need help fixing a "previously defined" error

From James Kuyper <jameskuyper@verizon.net>
Newsgroups comp.lang.c
Subject Re: Need help fixing a "previously defined" error
Date 2017-04-08 10:47 -0400
Organization A noiseless patient Spider
Message-ID <ocat16$nro$1@dont-email.me> (permalink)
References <20170408-130720.9.0@Peabody.ssl.astraweb.com>

Show all headers | View raw


On 04/08/2017 09:07 AM, Peabody wrote:
> As will shortly be obvious, I'm completely new to C.  I'm trying to 
> modify and recompile a Texas Instruments program called BSLDEMO2.exe 
> which is used to flash new firmware to some of their MSP430 
> microprocessors using a boot-strap loader. I need to invert the DTR line. 
> They provide the source code, which they compiled using a Microsoft 
> compiler.  I'm using LCC.  And this is C, not C++.
> 
> Before addressing the change I want to make, I'm just trying to get the 
> original source to compile in LCC.  I'm getting a "previously defined" 
> error that I don't quite know how to deal with.
> 
> The file is TI_TXT_Files.h, and it starts off this way:
...
> char* strtrim(char* lpszA);
...
> LCC says "strtrim" is previously defined in the #included <string.h>.
> 
> I guess the first question is what that char* line is doing. ...

It declares that strtrim identifies a function that takes one argument
of type char*, and returns a value of type char*. The "lpszA" is purely
decorative as far as C itself is concerned. It uses prefixes that tell
you something about the meaning of the string that it points at, but I'm
not familiar with those prefixes.

> ... The second 
> question is why didn't TI get this error when they compiled it. ...

Most likely because they used the Microsoft compiler, while you used
LCC. The C standard gives implementations a lot of freedom to differ
from each other, and "strtrim" is one example of that. strtrim() is NOT
part of the C standard library, so this might seem to be a
non-conforming implementation of <string.h>. However, 7.31.13 says that
names beginning with "str", "mem", or "wcs" followed by a lower case
letter may be added to future versions of <string.h>, and according to
7.1.3p1, all such names are therefore reserved for use as identifiers
with external linkage. strtrim matches that pattern. Because such names
are reserved, LCC is entitled to add such names to it's own version of
<stdio.h>, and the TI code has undefined behavior.
"Undefined behavior" means that Microsoft compiler is free to accept
such code and have it execute exactly as expected, but it also means
that LCC is allowed to reject such code.

> ...  But 
> the main question is how to fix it.
> 
> LCC's string.h has this:
> 
> #ifndef __ANSIC__ONLY__
...
> int      strtrim(char *);           <============= here
...
> #endif
> 
> On the surface there seems to be a conflict between the char* and the int 
> declarations. ...

That is precisely the conflict that is causing the problem. There are
implementations where char* and int can be successfully type-punned for
each other, but it's not anything you can count on portably. They're not
even remotely compatible types as far as the C standard is concerned.

> There are no other instances of "strtrim" appearing anywhere in the TI 
> source code.

The TI code declares strtrim(), but makes no use of it? That's weird,
but unused declarations are not uncommon in code that has a lot of
history. If so, that's trivial to deal with - just remove the declaration.
If you made a mistake while searching for other uses of strtrim,
removing that declaration will help you find them. If you do end up
finding some, the problem gets much more complicated. In that case, let
us know precisely how it is used, and we might be able to suggest a
work-around.

> It seems that the safest way to deal with this might be to make a copy of 
> string.h with the strtrim declaration commented out, and move it up to 
> the source code folder, and change all <string.h> #includes to 
> "string.h".  Would that work?  Is there a better way?

Modifying standard headers is job to be done only by experts, and most
experts know enough to not do it. You're much better off changing the TI
code.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Need help fixing a "previously defined" error Peabody <waybackNO584SPAM44@yahoo.com> - 2017-04-08 08:07 -0500
  Re: Need help fixing a "previously defined" error James Kuyper <jameskuyper@verizon.net> - 2017-04-08 10:47 -0400
    Re: Need help fixing a "previously defined" error Peabody <waybackNO584SPAM44@yahoo.com> - 2017-04-08 12:51 -0500
      Re: Need help fixing a "previously defined" error Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-04-08 20:03 +0100
      Re: Need help fixing a "previously defined" error Spiros Bousbouras <spibou@gmail.com> - 2017-04-08 19:06 +0000
      Re: Need help fixing a "previously defined" error James Kuyper <jameskuyper@verizon.net> - 2017-04-08 15:17 -0400
  Re: Need help fixing a "previously defined" error Spiros Bousbouras <spibou@gmail.com> - 2017-04-08 15:29 +0000
  Re: Need help fixing a "previously defined" error Barry Schwarz <schwarzb@dqel.com> - 2017-04-08 09:21 -0700
    Re: Need help fixing a "previously defined" error Peabody <waybackNO584SPAM44@yahoo.com> - 2017-04-08 13:04 -0500
      Re: Need help fixing a "previously defined" error James Kuyper <jameskuyper@verizon.net> - 2017-04-08 15:33 -0400
        Re: Need help fixing a "previously defined" error Spiros Bousbouras <spibou@gmail.com> - 2017-04-08 19:51 +0000
        Re: Need help fixing a "previously defined" error Peabody <waybackNO584SPAM44@yahoo.com> - 2017-04-08 16:24 -0500
    Re: Need help fixing a "previously defined" error Keith Thompson <kst-u@mib.org> - 2017-04-08 18:45 -0700
  Re: Need help fixing a "previously defined" error bartc <bc@freeuk.com> - 2017-04-08 17:46 +0100
    Re: Need help fixing a "previously defined" error Peabody <waybackNO584SPAM44@yahoo.com> - 2017-04-08 13:19 -0500
  Re: Need help fixing a "previously defined" error David Brown <david.brown@hesbynett.no> - 2017-04-09 12:38 +0200
    Re: Need help fixing a "previously defined" error Peabody <waybackNO584SPAM44@yahoo.com> - 2017-04-09 07:52 -0500
      Re: Need help fixing a "previously defined" error David Brown <david.brown@hesbynett.no> - 2017-04-09 17:02 +0200
        Re: Need help fixing a "previously defined" error Peabody <waybackNO584SPAM44@yahoo.com> - 2017-04-09 18:20 -0500
          Re: Need help fixing a "previously defined" error James Kuyper <jameskuyper@verizon.net> - 2017-04-09 22:06 -0400
          Re: Need help fixing a "previously defined" error David Brown <david.brown@hesbynett.no> - 2017-04-10 09:07 +0200
            Re: Need help fixing a "previously defined" error Peabody <waybackNO584SPAM44@yahoo.com> - 2017-04-10 09:11 -0500
              Re: Need help fixing a "previously defined" error David Brown <david.brown@hesbynett.no> - 2017-04-10 17:31 +0200
                Re: Need help fixing a "previously defined" error Peabody <waybackNO584SPAM44@yahoo.com> - 2017-04-10 12:48 -0500

csiph-web