我目前正在 linux 上链接两个第三方共享库(A.so 和 B.so)。问题是这两个 so 都与另一个库静态链接,因此来自 A.so 和 B.so 的大约 400 个函数具有相同的名称。当我用 -lA -lB 或 -lB -lA 编译和链接时,根据顺序,由于函数插入导致问题和代码无法运行,函数分别从 A 或 B 中提取。我想知道是否有办法将函数名称绑定到它们的库,以便可以链接和使用这两个库?因为那些重叠的函数名是在 A 和 B 内部调用的,所以我不能使用像 objcopy 之类的东西。 dlopen 会有帮助吗?
我在 C++11 中实现线程,并且每当我从 if 语句中启动线程时都会遇到编译问题。
我收到的错误是:
file.cpp: In function ‘int main(int, char**)’:
file.cpp:16:2: error: ‘thread1’ was not declared in this scope
thread1.join();
Run Code Online (Sandbox Code Playgroud)
当我将线程移到 if 语句之外时,一切都会编译并运行良好。
我正在使用 g++ 版本 4.8.2 并使用 -std=c++11 编译器选项。
这段代码不会编译
#include <unistd.h>
#include <thread>
#include <iostream>
void testthread() {
std::cout << "Thread was run" << std::endl;
}
int main(int argc, char**argv) {
if (true) {
std::thread thread1(testthread);
}
sleep(1);
thread1.join();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
此代码按预期编译并运行
#include <unistd.h>
#include <thread>
#include <iostream>
void testthread() {
std::cout << "Thread was …Run Code Online (Sandbox Code Playgroud) 我试图理解一个简单的 C++ 程序的汇编输出。这是我的 C++ 程序。
void func()
{}
int main()
{
func();
}
Run Code Online (Sandbox Code Playgroud)
当我使用带有 --save-temps 选项的 g++ 来获取上述程序的汇编代码时,我得到以下汇编代码。
.file "main.cpp"
.text
.globl _Z4funcv
.type _Z4funcv, @function
_Z4funcv:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.size _Z4funcv, .-_Z4funcv
.globl main
.type main, @function
main:
.LFB1:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
call _Z4funcv
movl $0, %eax
popq %rbp
.cfi_def_cfa …Run Code Online (Sandbox Code Playgroud) 为了确定给定文件的视频持续时间,我使用 libavformat。我的程序如下所示:
#include <stdio.h>
#include <libavformat/avformat.h>
#include <libavutil/dict.h>
int main (int argc, char **argv) {
AVFormatContext *fmt_ctx = NULL;
int ret;
if (argc != 2) {
printf("usage: %s <input_file>\n", argv[0]);
return 1;
}
av_register_all();
if ((ret = avformat_open_input(&fmt_ctx, argv[1], NULL, NULL)))
return ret;
int64_t duration = fmt_ctx->duration;
int hours, mins, secs;
secs = duration / AV_TIME_BASE;
mins = secs / 60;
secs %= 60;
hours = mins / 60;
mins %= 60;
printf("Duration: %02d:%02d:%02d\n", hours, mins, secs);
avformat_free_context(fmt_ctx);
return 0; …Run Code Online (Sandbox Code Playgroud) 我正在尝试编译 C 和 C++ 文件并将它们链接在一起。我正在关注此链接中的答案 -使用 GCC 编译 C 和 C++ 文件
但是我有一个不同的问题,在那个帖子中没有解释。我已经在 C++ 文件中定义了我的 main() 并使用了一个函数,其详细信息在 C 文件中。该函数的声明位于 .h 文件中,该文件包含在 C 和 C++ 文件中。
我的 C++ 文件 -
#include<iostream>
#include<testc.h>
using namespace std;
extern "C" void cfunc(int, int);
int main()
{
cout<<"Hello from cpp"<<endl;
cfunc(3,6);
}
Run Code Online (Sandbox Code Playgroud)
我的 C 文件 -
#include<stdio.h>
#include<testc.h>
int cfunc2(int a, int b)
{
return a+b;
}
void cfunc(int a, int b)
{
printf("Hello from c %d\n",cfunc2(a,b));
}
Run Code Online (Sandbox Code Playgroud)
我的 .h 文件 -
int cfunc2(int, …Run Code Online (Sandbox Code Playgroud) foo.cpp:
#define ID A
#if ID == A
#warning "hello, world"
#endif
Run Code Online (Sandbox Code Playgroud)
编译g++ -c foo.cpp工作正常:(g ++ v8.2.0)
foo.cpp:3:2: warning: #warning "hello, world" [-Wcpp]
#warning "hello, world"
^~~~~~~
Run Code Online (Sandbox Code Playgroud)
现在,如果我取代#define ID A用#define *,然后我得到:
foo.cpp:1:12: error: operator '*' has no left operand
#define ID *
^
foo.cpp:2:5: note: in expansion of macro ‘ID’
#if ID == A
^~
Run Code Online (Sandbox Code Playgroud)
有什么特别之处*?为什么它在#if表达式中失败?
我在C ++中编写一个512位整数。对于整数,我使用new关键字从堆中分配内存,但是编译器(MINGW上的g ++版本8.1)似乎错误地对其进行了优化。即编译器命令是:
g++ -Wall -fexceptions -Og -g -fopenmp -std=c++14 -c main.cpp -o main.o
g++ -o bin\Debug\cs.exe obj\Debug\main.o -O0 -lgomp
码:
#include <iostream>
#include <cstdint>
#include <omp.h>
constexpr unsigned char arr_size = 16;
constexpr unsigned char arr_size_half = 8;
void exit(int);
struct uint512_t{
uint32_t * bytes;
uint512_t(uint32_t num){
//The line below is either (wrongfully) ignored or (wrongfully) optimized out
bytes = new(std::nothrow) uint32_t[arr_size];
if(!bytes){
std::cerr << "Error - not enough memory available.";
exit(-1);
}
*bytes = num;
for(uint32_t …Run Code Online (Sandbox Code Playgroud) 我尝试在c ++中编译一个简单的c ++代码,但是当我尝试在Windows中使用g ++编译它时,总是返回错误。
我用
g++ -std=c++0x -pthread main.cpp
Run Code Online (Sandbox Code Playgroud)
错误消息是:
std::thread' is defined in header '<thread>'; did you forget to '#include <thread>'?
Run Code Online (Sandbox Code Playgroud)
这没有意义,因为代码只是
#include<thread>
void f(int i) {}
int main() {
std::thread t(f, 1);
t.join();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我相信这段代码可以在linux上运行,我想知道为什么它不能在Windows下运行。
当我尝试在Linux中使用G ++ 4.8编译程序时出现错误“未提供有效的预处理令牌”。并且在Solaris中使用CCSuntudio进行编译时没有错误。
在我的代码下面:
#include <iostream>
#define func(type1,varname1) \
cout << "ma var est "<<##varname1<<" et le type est "<<#type1; \
cout <<endl;
using namespace std;
int main() {
func("int", "area");
}
Run Code Online (Sandbox Code Playgroud)
它可以在CCSunStudio中完美运行,但不适用于G ++
hello.hxx:2:23: error: pasting "<<" and ""area"" does not give a valid preprocessing token
cout << "ma var est "<<##varname1<<" et le type est "<<#type1; \
^
hello.cxx:7:1: note: in expansion of macro ‘func’
func("int","area");
^
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助
这是在utils.hpp(NON OOP,所以不在任何类中)定义的函数原型声明
static void create_xy_table(const k4a_calibration_t *calibration, k4a_image_t xy_table);
static void generate_point_cloud(const k4a_image_t depth_image,
const k4a_image_t xy_table,
k4a_image_t point_cloud,
int *point_count);
static void write_point_cloud(const char *file_name, const k4a_image_t point_cloud, int point_count);
Run Code Online (Sandbox Code Playgroud)
我对它们的定义utils.cpp包括utils.hpp。
当我使用中的函数调用任何main函数时main.cpp,出现以下错误:
/tmp/ccFcMfYB.o: In function `main':
main.cpp:(.text+0x208): undefined reference to
`create_xy_table(_k4a_calibration_t const*, _k4a_image_t*)
Run Code Online (Sandbox Code Playgroud)
编译命令:
g++ -o build/testtest src/main.cpp src/utils.cpp -I./include -lk4a
将所有函数都定义为非静态,并且可以完美地进行编译!!我无法解决这个问题。
我的系统配置:gcc版本7.4.0(Ubuntu 7.4.0-1ubuntu1〜18.04.1)
编辑:这是我在utils.cpp中的函数定义:
static void create_xy_table(const k4a_calibration_t *calibration, k4a_image_t xy_table)
{
k4a_float2_t *table_data …Run Code Online (Sandbox Code Playgroud) g++ ×10
c++ ×9
gcc ×3
c++11 ×2
preprocessor ×2
assembly ×1
c ×1
compilation ×1
libavformat ×1
linux ×1
new-operator ×1