Jason Pan

gcc 常见技巧和错误/告警修复

黄杰 / 2020-10-24


打印所有的 define 宏定义

gcc -dM -E - < /dev/null
g++ -dM -E -x c++ - < /dev/null

If you use -dM without the -E option, -dM is interpreted as a synonym for -fdump-rtl-mach.

tc_pack.h:94:76: warning: throw will always call terminate() [-Wterminate]
   94 |    throw TC_PackIn_Exception("TC_PackIn cur has beyond error.");
      |                                                               ^
tc_pack.h:94:76: note: in C++11 destructors default to noexcept
hiredis.c: In function 'redisContext* redisContextInit()':
hiredis.c:596:15: warning: invalid conversion from 'void*' to 'redisContext*' [-fpermissive]
  596 |     c = calloc(1,sizeof(redisContext));
      |         ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
      |               |
      |               void*
cmcc_client_mgr.h: In member function 'int cmcc::ITOPClientKeeper::RealRun()':
cmcc_client_mgr.h:157:21: warning: variable 'ret' set but not used [-Wunused-but-set-variable]
  157 |          int ret = 0;
      |              ^~~
tc_http_async.cpp: In member function 'int taf::TC_HttpAsync::setProxyAddr(const char*)':
tc_http_async.cpp:428:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
  428 |     if(v.size() < 2)
      |     ^~
tc_http_async.cpp:431:2: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
  431 |  return setProxyAddr(v[0].c_str(), TC_Common::strto<uint16_t>(v[1]));
      |  ^~~~~~
cannot declare member function ‘static bool Utils::CheckInSemicolonArray(std::string, const T&)’ to have static linkage [-fpermissive]
  214 | static bool Utils::CheckInSemicolonArray(std::string s, const T& needle) {

模板未实例化

header file:

template<typename T>
static bool CheckInSemicolonArray(std::string s, const T& needle);

used in src:

CheckInSemicolonArray(allowed_referers, client_referer_host_)

编译通过,但是nm没有对应的符号链接:

$ nm liblogin_svr.so | grep CheckInSemicolonArray
                 U _ZN5Utils21CheckInSemicolonArrayISsEEbSsRKT_
                 U _ZN5Utils21CheckInSemicolonArrayIiEEbSsRKT_

需要实例化函数模板

template bool Utils::CheckInSemicolonArray(std::string s, const std::string&);
template bool Utils::CheckInSemicolonArray(std::string s, const int&);

这个实例化放在哪里?

sacc_login_msg.cpp: In instantiation of 'static bool Utils::CheckInSemicolonArray(std::string, const T&) [with T = std::string]':
sacc_login_msg.cpp:9:   instantiated from here
sacc_login_msg.cpp:9: error: explicit instantiation of 'static bool Utils::CheckInSemicolonArray(std::string, const T&) [with T = std::string]' but no definition available

https://stackoverflow.com/questions/115703/storing-c-template-function-definitions-in-a-cpp-file

https://isocpp.org/wiki/faq/templates#templates-defn-vs-decl

template <class A>
class T
{
private:
    static T t;
public:
    T& getT() {return t;}
};

上边这个是什么含义?

运行时错误

basic_string::substr: __pos (which is 3) > this->size() (which is 2)

Given an integer array data representing the data, return whether it is a valid UTF-8 encoding.

A character in UTF8 can be from 1 to 4 bytes long, subjected to the following rules:

  1. For a 1-byte character, the first bit is a 0, followed by its Unicode code.
  2. For an n-bytes character, the first n bits are all one’s, the n + 1 bit is 0, followed by n - 1 bytes with the most significant 2 bits being 10.

This is how the UTF-8 encoding would work:

   Char. number range  |        UTF-8 octet sequence
      (hexadecimal)    |              (binary)
   --------------------+---------------------------------------------
   0000 0000-0000 007F | 0xxxxxxx
   0000 0080-0000 07FF | 110xxxxx 10xxxxxx
   0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
   0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx

Note: The input is an array of integers. Only the least significant 8 bits of each integer is used to store the data. This means each integer represents only 1 byte of data.

Example 1:

Input: data = [197,130,1]
Output: true
Explanation: data represents the octet sequence: 11000101 10000010 00000001.
It is a valid utf-8 encoding for a 2-bytes character followed by a 1-byte character.

Example 2:

Input: data = [235,140,4]
Output: false
Explanation: data represented the octet sequence: 11101011 10001100 00000100.
The first 3 bits are all one's and the 4th bit is 0 means it is a 3-bytes character.
The next byte is a continuation byte which starts with 10 and that's correct.
But the second continuation byte does not start with 10, so it is invalid.

Constraints:

Accepted

external/astc-encoder/Source/stb_image.h:2392:43: error: implicit conversion from 'float' to 'double' to match other operand of binary expression [-Werror=double-promotion]
 2392 | #define stbi__f2f(x)  ((int) (((x) * 4096 + 0.5)))
      |                                ~~~~~~~~~~~^~~~~
src/test_google_test.cc:15:1:   required from here
external/com_google_googletest/googletest/include/gtest/internal/gtest-internal.h:523:29: error: 'static void testing::Test::SetUpTestCase()' is inaccessible within this context
  523 |         GetNotDefaultOrNull(&T::SetUpTestCase, &Test::SetUpTestCase);
      |                             ^~~~~~~~~~~~~~~~~
In file included from src/test_google_test.cc:1:
external/com_google_googletest/googletest/include/gtest/gtest.h:444:15: note: declared here
  444 |   static void SetUpTestCase() {}
      |               ^~~~~~~~~~~~~