chili3d笔记4 emcc编译c++ 研究cmake json +emsctrpten学习+emsdk,c++项目内加入三方库

发布于:2025-04-22 ⋅ 阅读:(21) ⋅ 点赞:(0)
{
    "typescript.tsdk": "node_modules/typescript/lib",
    "editor.codeActionsOnSave": {
        "source.organizeImports": "explicit"
    },
    "editor.inlineSuggest.showToolbar": "onHover",
    "cmake.sourceDirectory": "${workspaceFolder}/cpp",
    "C_Cpp.default.configurationProvider": "ms-vscode.cpptools"
}

{
    "compilerOptions": {
        "target": "ESNext",
        "allowJs": true,
        "module": "ESNext",
        "moduleResolution": "Node",
        "noImplicitOverride": true,
        "noPropertyAccessFromIndexSignature": true,
        "experimentalDecorators": true,
        "forceConsistentCasingInFileNames": true,
        "strict": true,
        "esModuleInterop": true,
        "resolveJsonModule": true,
        "plugins": [{ "name": "typescript-plugin-css-modules" }]
    },
    "include": ["packages"],
    "exclude": ["packages/chili-occ/occ-wasm", "packages/chili-wasm/lib"]
}

 message(STATUS "Building with Emscripten support enabled.")

编不起来换个编法

git clone https://github.com/ticket180/chili3d.git -b njsgcs

原来 node_modules这么来的

怎么单独编译cpp文件,总不能我每次报个错都得全部编译一遍吧

 

export PATH=$PATH:/usr/lib/emscripten
export EMSCRIPTEN=/usr/lib/emscripten
echo $PATH
echo $EMSCRIPTEN

emcc -I/usr/include llm.cpp -o llm.html

emcc llm.cpp -o llm.html

 

 

奇怪的知识增加了

参考Ubuntu 编译出现fatal error: bits/libc-header-start.h: No such file or directory-CSDN博客

 

sudo apt-get install gcc-multilib

写个HELLOW WORLD得了

  参考集成Emscripten+wasm至React项目踩坑记录_wasm streaming compile failed: typeerror: failed t-CSDN博客

emcc helloworld.cpp -o hello.js -s EXPORTED_FUNCTIONS=_add -s EXPORTED_RUNTIME_METHODS=ccall,cwrap -sMODULARIZE

怕不是下了个假版的emscripton

nano ~/.bashrc
git clone https://github.com/emscripten-core/emsdk.git
cd emsdk

./emsdk update
./emsdk install latest
./emsdk activate latest
source /home/chen/代码/emsdk/emsdk_env.sh

参考:https://github.com/emscripten-core/emscripten/issues/21901

有时候跑通了就是跑通了

参考:WebAssembly编译指南_emscripten重新编译整个项目-CSDN博客

 

emcc example.cpp -o example.html

 

 python -m http.server 8000

<body>
<h1>Hello World test</h1>
<script type="text/javascript" src="hello.js"></script>
</body> 

 

 

 

sato77711/try_emcc

 

 

<body>
    <h1>Hello World test</h1>
    <script type="text/javascript" src="index.js"></script>
    <script>

        Module.onRuntimeInitialized = function() {
           console.log("aa"+Module._add(10,20));
        }
        
       </script>
    </body> 
    

 

#include <iostream>


extern "C" {
int add(int x, int y){
       return x+y;
    }
int sub(int x, int y){
         return x-y; 
    }
 
  }

评价是不如直接写成js,用emcc导模块还会有奇怪的bug

测试下包

单加/user/include是会报错就不能用绝对地址定位nlohmann了

submodule命令

git submodule add https://github.com/nlohmann/json.git third_party/nlohmann_json

复制到项目底下

#include <emscripten/fetch.h>

把curl改成fetch

emcc 编译c++返回 string怎么办emcc不能用std命名空间

参考:https://segmentfault.com/a/1190000011229465

单独测试binding功能

这里查不到对应名称

my_module的位置可以什么也不写

参考:webassembly链接C++和JS——embind-CSDN博客

task里面发服务器会堵塞进程


 

    // quick_example.cpp
    #include <emscripten/bind.h>
#include<iostream>
    using namespace emscripten;

    EMSCRIPTEN_KEEPALIVE float myadd(float a, float b) {

      std::cout<<"hello world233"<<std::endl;
        return a+b;
    }
    EMSCRIPTEN_BINDINGS() {
     
        function("epsasaasmyadd",&myadd);
    }

如果EMSCRIPTEN_KEEPALIVE,的话函数可以在json里面找到但是名字变得很奇怪

用fetch加fetch

好像只能web调试

 c++写js代码翻译成js


    emscripten_run_script(R"(
        fetch('https://api.deepseek.com/v1/chat/completions', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'Authorization': 'Bearer aaaa'
            },
            body: JSON.stringify({
                model: 'deepseek-chat',
                messages: [{ role: 'system', content: 'you are a good assistant.' }, { role: 'user', content: '你好,DeepSeek!' }]
            })
        }).then(response => response.json())
          .then(data => console.log(data))
          .catch(error => console.error('Error:', error));
    )");

Emscripten教程之连接C++和JavaScript(三) - h2z - 博客园

拿foxapi代码跟ai换c++代码

无法返回EMSCRIPTEN_KEEPALIVE std::string,可以返回int

 os t

同义林吗刷新要用newtask,clearcontent文件还是读取旧的文件

deepseek不能接受localhost的请求

#include <iostream>
#include <string>
#include <cstdlib> // For getenv
#include <thread>
#include <chrono>
#include "third_party/nlohmann_json/include/nlohmann/json.hpp"
#include "third_party/httplib/httplib.h"
#include <emscripten/bind.h>
#include <emscripten/val.h>
#include <emscripten/emscripten.h>

using json = nlohmann::json;
using namespace emscripten;

EMSCRIPTEN_KEEPALIVE std::string send_to_llm(std::string message) {
    std::cout << "Sending message: " << message << std::endl;
    // 创建客户端
    httplib::Client cli("https://open.bigmodel.cn/");

    // 设置请求头
    httplib::Headers headers = {
        {"Authorization", "Bearer d121416576624afca2902462fda1baff.Va2AyE6qDFzYTmZc"},
        {"Content-Type", "application/json"}
    };

    // 构造 JSON 数据
    json data = {
        {"model", "glm-4-flash"},
        {"messages", {
            {{"role", "user"}, {"content", "Hello"}},
            {{"role", "system"}, {"content", "you are a helpful assistant"}}
        }}
    };

    // 发送 POST 请求
    auto res = cli.Post("api/paas/v4/chat/completions", headers, data.dump(), "application/json");

    // 检查响应
    std::string response;
    if (res && res->status == 200) {
        std::cout << "Response: " << res->body << std::endl;
        response = res->body;
    } else {
        std::cerr << "Error: " << (res ? std::to_string(res->status) : "No response") << std::endl;
        response = "";
    }
    return  response ; // 返回指针

}
EMSCRIPTEN_BINDINGS() {
    function("send_to_llm", &send_to_llm);
}

<button οnclick="callCPlusPlusFunction()">Call C++ Function</button>       /结束符号

 

 

 


网站公告

今日签到

点亮在社区的每一天
去签到