./libmylib.so:未定义的"submarinex::LIB::kCount"
./libmylib.so: undefined reference to `submarinex::LIB::kCount'
我在目录中有3个文件。我将构建ll.cc
至libmylib.so
,然后将main.cc
构建为myexe
。
使用这些命令构建
g++ -Wall -g -fPIC -std=c++11 ll.cc -shared -o libmylib.so
g++ -Wall -g -std=c++11 main.cc -L. -lmylib -o myexe
但是,G 报告构建myexe
时的错误:
./libmylib.so: undefined reference to `submarinex::LIB::kCount'
collect2: error: ld returned 1 exit status
文件:
ll.h
namespace submarinex {
class LIB {
public:
void Print();
private:
static const int kCount = 100;
};
} // namespace submarinex
ll.cc
#include "ll.h"
#include <algorithm>
#include <iostream>
namespace submarinex {
void LIB::Print() {
int min = std::min(101, kCount);
std::cout << min << std::endl;
// std::cout << kCount << std::endl;
}
} // namespace submarinex
main.cc
#include "ll.h"
int main(int argc, char **argv) {
submarinex::LIB lib;
lib.Print();
return 0;
}
Case 1
:如果在Print
中使用这2行,则将在main.cc
int min = std::min(101, kCount);
std::cout << min << std::endl;
Case 2
:如果在Print
中使用此行,将成功
std::cout << kCount << std::endl;
如果更改
static const int kCount = 100;
to
const int kCount = 100;
无论使用Case1
或Case2
。
我不知道如何解决这个问题。
我认为您的源代码中的kcount变量没有定义。
因此,您的变量必须在源代码中的某个地方定义,ll.cc。
在ll.h
中namespace submarinex {
class LIB {
private:
const static int kCount =100;
public:
void Print();
};
} // namespace submarinex
然后,您可以在ll.cc
中定义一个kcount变量#include "ll.h"
#include <algorithm>
#include <iostream>
namespace submarinex
{
const int LIB::kCount; //<== here
void LIB::Print() {
int min = std::min(101, kCount);
std::cout << min << std::endl;
// std::cout << kCount << std::endl;
}
} // namespace submarinex
这是我的GCC版本信息
$ gcc -v
Using built-in specs.
COLLECT_GCC=C:DEVCOMPmsys32mingw32bingcc.exe
COLLECT_LTO_WRAPPER=C:/DEV/COMP/msys32/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.3.0/lto-wrapper.exe
Target: i686-w64-mingw32
Configured with: ../gcc-6.3.0/configure --prefix=/mingw32 --with-local-prefix=/mingw32/local --build=i686-w64-mingw32 --host=i686-w64-mingw32 --target=i686-w64-mingw32 --with-native-system-header-dir=/mingw32/i686-w64-mingw32/include --libexecdir=/mingw32/lib --enable-bootstrap --with-arch=i686 --with-tune=generic --enable-languages=c,lto,c++,objc,obj-c++,fortran,ada --enable-shared --enable-static --enable-libatomic --enable-threads=posix --enable-graphite --enable-fully-dynamic-string --enable-libstdcxx-time=yes --disable-libstdcxx-pch --disable-libstdcxx-debug --disable-isl-version-check --enable-lto --enable-libgomp --disable-multilib --enable-checking=release --disable-rpath --disable-win32-registry --disable-nls --disable-werror --disable-symvers --with-libiconv --with-system-zlib --with-gmp=/mingw32 --with-mpfr=/mingw32 --with-mpc=/mingw32 --with-isl=/mingw32 --with-pkgversion='Rev1, Built by MSYS2 project' --with-bugurl=https://sourceforge.net/projects/msys2 --with-gnu-as --with-gnu-ld --disable-sjlj-exceptions --with-dwarf2
Thread model: posix
gcc version 6.3.0 (Rev1, Built by MSYS2 project)
用mingw32 shell编译。
g++ -Wall -g -fPIC -std=c++11 -I. ll.cc -shared -o libmylib.a
g++ -Wall -g -std=c++11 -I. -L. -lmylib main.cc -o myexe
我希望这对您有帮助。
问:
std::min
使用两个params const int&
(不是 int
),然后将引用传递给没有定义的submarinex::LIB::kCount
(您只是在.h
文件中声明)。
在这种情况下,您可以在ll.cc
namespace submarinex {
static const int LIB::kCount = 100; //define it here
void LIB::Print() {
std::min(101, kCount);
}
}
相关文章:
- 如何指定我希望我的LIB链接到的DLL文件?-Visual Studio 2019
- JSON转换为nlohmann JSON-lib中的结构数组
- 为什么导入Mixed native/CLR lib.dll的本机C++应用程序没有在Mixed lib.dll中的外部变
- 使用 VS2015 在 Windows 10 上构建 Fortran .lib x64 位并将其链接到 C++
- 为什么我的链接器在找到Coin4d.lib后搜索Coin4sd.lib?
- libcpmtd.lib有什么用?
- LNK1104:无法打开libpjproject-i386-Win32-vc14-Debug-Static.lib
- 解析引用 |exe/dll vs lib
- 在 /usr/local/lib 下找不到库
- LLVM 编译:目标的配方 '../lib/IR/Release+Asserts/Intrinsics.gen.tmp' failed
- CMake 生成的 MSVC 项目找不到符号,即使为其相关 dll 正确生成了 lib 文件也是如此
- VS2015 未链接用户32.lib
- 从 .lib 文件创建一个.dll文件
- 看不到文件'SDL2.lib'
- 如何使用用户定义的函数覆盖 C lib 函数,如 _sbrk?
- 如何在不需要LIBCD.lib的情况下在Visual Studio 6中编译C项目
- 可执行文件C++包括.obj,.lib和.dll吗?
- 我应该安装 .cmake 文件来共享/cmake 还是 lib/cmake?
- 无法在 DLL 中链接 SDL2-static.lib
- ./libmylib.so:未定义的"submarinex::LIB::kCount"