0

This is my first time encountering this type of linking error for 20 years while trying to use other people's code.

Here I will be brief and using abbreviated examples.

Say the file enums.hpp

==== content====
#ifndef _BLABLA_
#define _BLABLA_
enum SomeKind { BLACK, RED, GREEN }
static void parse(const std::string& s, SomeKind) {
   // definition
}
..... More enum, and static functions
#endif

In this file there are several enum and parse. Because of the STATIC keyword, the compiler will complain about the unused functions. I experimented by moving the definition of those function to a enum.cpp file. Then at link time, I am getting the error message:

undefined reference to `someNameSpace::SomeKind

One solution I will try to use the library as is (I will probably do that). This project (I am using) is a CMake project. What's a better way of organizing the original code to git rid of both problems: unused function, and undefined reference?

After removing the static label then it get rid of the linking problem of the enum. Essentially the new organization is as:

enums.hpp
enum SomeKind { }
void someFunction(SomeKind sk);

enums.cpp
void someFunction(SomeKind sk) {
// definition here
}

I think elimination of the static make the function visible and some how included in the linking stage. This is a big library, I have only a few hours looking into this library.

Kemin Zhou
  • 6,264
  • 2
  • 48
  • 56
  • 3
    Please try to create a [mcve] to show us, and while you [edit] your question then also please copy-paste the *full* and *complete* build output from that example. – Some programmer dude May 01 '20 at 21:25
  • 1
    "unused function" in a reusable library is not a problem that needs to be gotten rid of. You could look at ways to tell the static analyzer that it's ok for functions in this header to be unused (typically via `#pragma warning` to disable that warning for one block of code) – Ben Voigt May 01 '20 at 21:26
  • On an unrelated note, all symbols beginning with an underscore and followed by an upper-case letter (like e.g. `_BLABLA_`) are *reserved*. Please see [What are the rules about using an underscore in a C++ identifier?](https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) for details. – Some programmer dude May 01 '20 at 21:26
  • _BLABLA_ is just an illustration from the original header file. The original header file has a much longer one. – Kemin Zhou May 01 '20 at 21:29
  • The actual symbol or its length doesn't matter, just having a leading underscore followed by an upper-case letter makes it invalid. – Some programmer dude May 01 '20 at 21:34
  • This is other peope's library, I am evaluating whether to use it as starting point or not. there are a lots of places that are not following good practice. I don't want to show the original author's code directly here (using blabal to mean any other all cap string) – Kemin Zhou May 01 '20 at 21:39
  • @KeminZhou `moving the definition of those function to a enum.cpp file` Did that place the functions inside a namespace (or class) other than the original one(s), and is `SomeKind` also defined anywhere else in the code? – dxiv May 01 '20 at 22:10
  • The error message ``undefined reference to `someNameSpace::SomeKind`` is NOT about a function `parse` or `someFunction`. This message looks like a compiler treats a enumeration type as a class with a **constructor** and exactly a definition of a constructor is missed. BTW, the code shown is **not** even **compiled**: it lacks for semicolon between the definition of the enumeraton type and the declaration of the function. – Tsyvarev May 01 '20 at 22:12
  • Sorry for not making a real minimal code. I was directly playing with the large library which still have over 500 warnings. – Kemin Zhou May 01 '20 at 23:31
  • 1
    For us to be able to properly help you we really need a [mcve] which replicates the exact problem, and an exact copy-paste of the full and complete error output. If it's not possible to create a simple example (because your own attempt to do so seems to work) then it's even more important that you show us the exact error messages (it's okay to anonymize the file-names). If you can't provide either then it's really impossible to to more than *guess*, and guess *badly*. We really want to help you, but without proper information it's just impossible. – Some programmer dude May 02 '20 at 02:32
  • All in all, the code you show should not cause a *linker* error. Enumerations, strongly typed enumerations (`enum class`) should be handled all at compile-time, and should have been replaced by their corresponding integer values by the compiler before the translation unit is passed to the linker. Therefore the error message you show can't be real, unless there's something you forgot to tell us. You can't get linker error for enumerations, only for functions using enumerations. – Some programmer dude May 02 '20 at 02:35

0 Answers0