Cgo 找不到像 <iostream> 这样的标准库
Cgo can't find standard libraries like <iostream>
我试图在我的 Go 代码中包含 C++ 代码,但无法识别。
我首先认为它认为它是 C 代码并尝试(并失败)编译,但删除包含行实际上给了我这样的 C++ 错误故障排除
error: ‘cout’ is not a member of ‘std’
代码使用 g++ 正确编译。
我尝试添加 -lstdc++ LDLFLAG,并在 CXXFLAG 中添加库的路径,但它没有改变任何东西。
我做了一些其他测试(都失败了)但这是最小的一个。
这是c++文件
test.cpp
#include "test.hpp"
int test()
{
std::cout << "Hello, World! ";
return 0;
}
test.hpp
#include <iostream>
int test() ;
这是我的go文件
//#cgo CXXFLAGS: -I/usr/lib/
//#cgo LDFLAGS: -L/usr/lib/ -lstdc++
//#include "test.hpp"
import "C"
func main() {
C.test()
}
我使用 go build
编译,但我也尝试使用 env CGO_ENABLED CGO_CXXFLAGS="-std=c++11" go build
(env 部分是特定于鱼的)并且它 returns 同样的错误。
它应该可以正确编译,但我有 iostream: No such file or directory
。
编辑:
我尝试按照评论中的建议添加 CFLAGS: -x c++
,编译器在正确的位置搜索,但我得到另一个错误 invalid conversion from ‘void*’ to ‘_cgo_96e70225d9dd_Cfunc_test(void*)::<unnamed struct>*’ [-fpermissive]
,我不知道它是否与这个新的 flafg
cgo 使得用 Go 包装 C 变得非常容易,但 C++ 有点不同。您必须 extern "C"
您想要的功能 make a function-name in C++ have 'C' linkage, otherwise the linker won't see the function. So, the actual problem is in the C++ header file. If you can't change the C++ code because it's a library, you may have to write wrappers (example).
这将编译:
.
├── test.cpp
├── test.go
└── test.hpp
test.hpp
#ifdef __cplusplus
extern "C" {
#endif
int test();
#ifdef __cplusplus
}
#endif
test.cpp
#include <iostream>
#include "test.hpp"
int test() {
std::cout << "Hello, World! ";
return 0;
}
test.go
package main
// #cgo CXXFLAGS: -I/usr/lib/
// #cgo LDFLAGS: -L/usr/lib/ -lstdc++
// #include "test.hpp"
import "C"
func main() {
C.test()
}
将文件放在同一个文件夹中,
运行go build
Hello, World!
我试图在我的 Go 代码中包含 C++ 代码,但无法识别。
我首先认为它认为它是 C 代码并尝试(并失败)编译,但删除包含行实际上给了我这样的 C++ 错误故障排除
error: ‘cout’ is not a member of ‘std’
代码使用 g++ 正确编译。
我尝试添加 -lstdc++ LDLFLAG,并在 CXXFLAG 中添加库的路径,但它没有改变任何东西。
我做了一些其他测试(都失败了)但这是最小的一个。
这是c++文件
test.cpp
#include "test.hpp"
int test()
{
std::cout << "Hello, World! ";
return 0;
}
test.hpp
#include <iostream>
int test() ;
这是我的go文件
//#cgo CXXFLAGS: -I/usr/lib/
//#cgo LDFLAGS: -L/usr/lib/ -lstdc++
//#include "test.hpp"
import "C"
func main() {
C.test()
}
我使用 go build
编译,但我也尝试使用 env CGO_ENABLED CGO_CXXFLAGS="-std=c++11" go build
(env 部分是特定于鱼的)并且它 returns 同样的错误。
它应该可以正确编译,但我有 iostream: No such file or directory
。
编辑:
我尝试按照评论中的建议添加 CFLAGS: -x c++
,编译器在正确的位置搜索,但我得到另一个错误 invalid conversion from ‘void*’ to ‘_cgo_96e70225d9dd_Cfunc_test(void*)::<unnamed struct>*’ [-fpermissive]
,我不知道它是否与这个新的 flafg
cgo 使得用 Go 包装 C 变得非常容易,但 C++ 有点不同。您必须 extern "C"
您想要的功能 make a function-name in C++ have 'C' linkage, otherwise the linker won't see the function. So, the actual problem is in the C++ header file. If you can't change the C++ code because it's a library, you may have to write wrappers (example).
这将编译:
.
├── test.cpp
├── test.go
└── test.hpp
test.hpp
#ifdef __cplusplus
extern "C" {
#endif
int test();
#ifdef __cplusplus
}
#endif
test.cpp
#include <iostream>
#include "test.hpp"
int test() {
std::cout << "Hello, World! ";
return 0;
}
test.go
package main
// #cgo CXXFLAGS: -I/usr/lib/
// #cgo LDFLAGS: -L/usr/lib/ -lstdc++
// #include "test.hpp"
import "C"
func main() {
C.test()
}
将文件放在同一个文件夹中,
运行go build
Hello, World!