内容的栏目 * @param int $category 0列表 1频道 2单页 3外链 * @return array */ function category_list($forumlist, $model = 0, $display = 0, $category = 0) { if (empty($forumlist)) return NULL; static $cache = array(); $key = $model . '-' . $display . '-' . $category; if (isset($cache[$key])) return $cache[$key]; if ($display) { foreach ($forumlist as $k => $val) { if (1 == $val['display'] && 1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } else { foreach ($forumlist as $k => $val) { if (1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } return empty($cache[$key]) ? NULL : $cache[$key]; } /** * @param $forumlist 所有版块列表 不分模型 * @param int $display 0全部CMS栏目 1在首页和频道显示内容的栏目 * @param int $category 0列表 1频道 2单页 3外链 * @return array */ function category_list_show($forumlist, $display = 0, $category = 0) { if (empty($forumlist)) return NULL; static $cache = array(); $key = $display . '-' . $category; if (isset($cache[$key])) return $cache[$key]; if ($display) { foreach ($forumlist as $k => $val) { if (1 == $val['display'] && 1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } else { foreach ($forumlist as $k => $val) { if (1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } return empty($cache[$key]) ? NULL : $cache[$key]; } /** * @param $forumlist 所有版块列表 * @return mixed BBS栏目数据(仅列表) 尚未开放bbs频道功能 */ function forum_list($forumlist) { if (empty($forumlist)) return array(); static $cache = array(); if (isset($cache['bbs_forum_list'])) return $cache['bbs_forum_list']; $cache['bbs_forum_list'] = array(); foreach ($forumlist as $_fid => $_forum) { if ($_forum['type']) continue; $cache['bbs_forum_list'][$_fid] = $_forum; } return $cache['bbs_forum_list']; } // 导航显示的版块 function nav_list($forumlist) { if (empty($forumlist)) return NULL; static $cache = array(); if (isset($cache['nav_list'])) return $cache['nav_list']; foreach ($forumlist as $fid => $forum) { if (0 == $forum['nav_display']) { unset($forumlist[$fid]); } } return $cache['nav_list'] = $forumlist; } ?>C++ emscripten setup - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

C++ emscripten setup - Stack Overflow

programmeradmin0浏览0评论

I'm trying to set up a simple c++ program using Visual Studio 2022. I want it to use emscripten.

After taking a long time trying to follow instructions on how to install emscripten, I'm not completely sure that it's correct. I read that I should check if emcc is setup from my command line, which I show here

c:\Users\emcc -v

emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 4.0.2 (7591f1c5ea0adf6f4293cfba2995ee9700aa0d93)
clang version 21.0.0git (https:/github/llvm/llvm-project 9534d27e3321a3b9e6e79fe6328445575bf26b7b)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: C:\emsdk\upstream\bin

Now that this is taken care of, I have no idea how to use this in my program. Instructions aren't clear at all anywhere I looked, and I can use some advice from programmers that know all this.

Thanks ahead for your comments!

--------- UPDATE ---------

Sorry about fetting to show the instructions I read. It slipped my mind when I was writing this message.

I tried 2 methods to install it. The first one was installing the visual studio 2022 Emscripten Build Target, which I found at .emscripten-build-support. It seemed to install correctly, but when I created 2 new projects (1 using the "Plain Emsripten Project" template, the other an empty c++ project), neither of them recognized "stdio.h" and "emscripten.h" The mentioned template had the the following

#include <stdio.h>
#include <emscripten.h>

int main() {
    printf("Hello, world!");
    return 0;
}

It gives the error "cannot open source file 'stdio.h' as well as 'emscripten.h'. It must have been a successful installation, seeing that it had this new template that didn't exist before, but it doesn't know where to find those .h files, and I didn't want to spend a lot of time trying to find them.

The second method I tried is at the website .html. For this, everything installed smoothly and I was able to find the .h files, but I get a ton of errors when I try the following code.

The code:

#include <C:\emsdk\upstream\lib\clang\21\include\llvm_libc_wrappers\stdio.h>
#include <C:\emsdk\upstream\emscripten\system\include\emscripten\emscripten.h>

int main() {
    printf("Hello, world!");
    return 0;
}

The errors:

cannot open source file emscripten/em_macros.h
cannot open source file stdbool.h
cannot open source file stdbool.h
identifier printf is undefined
cannot open source file stdlib.h
cannot open source file "stdio.h
#error directive: This file is for GPU offloading compilation only
The specified task executable location \upstream\emscripten\\emcc.bat is invalid.
#error directive: Including files directly from the emscripten source tree is not supported.  Please use the cache/sysroot/include directory.

I'm trying to set up a simple c++ program using Visual Studio 2022. I want it to use emscripten.

After taking a long time trying to follow instructions on how to install emscripten, I'm not completely sure that it's correct. I read that I should check if emcc is setup from my command line, which I show here

c:\Users\emcc -v

emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 4.0.2 (7591f1c5ea0adf6f4293cfba2995ee9700aa0d93)
clang version 21.0.0git (https:/github/llvm/llvm-project 9534d27e3321a3b9e6e79fe6328445575bf26b7b)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: C:\emsdk\upstream\bin

Now that this is taken care of, I have no idea how to use this in my program. Instructions aren't clear at all anywhere I looked, and I can use some advice from programmers that know all this.

Thanks ahead for your comments!

--------- UPDATE ---------

Sorry about fetting to show the instructions I read. It slipped my mind when I was writing this message.

I tried 2 methods to install it. The first one was installing the visual studio 2022 Emscripten Build Target, which I found at https://marketplace.visualstudio/items?itemName=KamenokoSoft.emscripten-build-support. It seemed to install correctly, but when I created 2 new projects (1 using the "Plain Emsripten Project" template, the other an empty c++ project), neither of them recognized "stdio.h" and "emscripten.h" The mentioned template had the the following

#include <stdio.h>
#include <emscripten.h>

int main() {
    printf("Hello, world!");
    return 0;
}

It gives the error "cannot open source file 'stdio.h' as well as 'emscripten.h'. It must have been a successful installation, seeing that it had this new template that didn't exist before, but it doesn't know where to find those .h files, and I didn't want to spend a lot of time trying to find them.

The second method I tried is at the website https://emscripten./docs/getting_started/downloads.html. For this, everything installed smoothly and I was able to find the .h files, but I get a ton of errors when I try the following code.

The code:

#include <C:\emsdk\upstream\lib\clang\21\include\llvm_libc_wrappers\stdio.h>
#include <C:\emsdk\upstream\emscripten\system\include\emscripten\emscripten.h>

int main() {
    printf("Hello, world!");
    return 0;
}

The errors:

cannot open source file emscripten/em_macros.h
cannot open source file stdbool.h
cannot open source file stdbool.h
identifier printf is undefined
cannot open source file stdlib.h
cannot open source file "stdio.h
#error directive: This file is for GPU offloading compilation only
The specified task executable location \upstream\emscripten\\emcc.bat is invalid.
#error directive: Including files directly from the emscripten source tree is not supported.  Please use the cache/sysroot/include directory.
Share Improve this question edited Feb 2 at 15:58 Steven Buechele asked Feb 2 at 4:17 Steven BuecheleSteven Buechele 271 silver badge3 bronze badges 3
  • marketplace.visualstudio/… gist.github/Roman-Port/8b03244e8c49abbf722b3196b4740c2c – Alan Birtles Commented Feb 2 at 6:03
  • First you need a proper build system, e.g. CMake (I assume you're using VS projects right now). Then use emcmake (see Emscripten manual). Making VS compile with Empscripten probably isn't trivial, I'd compile from the terminal. – HolyBlackCat Commented Feb 2 at 6:10
  • 1 this worked for me, try setting the Include and Library for emscripten to the Project Properties of your VS Project: C/C++ -> General for Additional Include Directories put the path from your command line output, using C:\emsdk\upstream\emscripten\system\include for the include directory, and Linker -> General for Additional Library Directories put your path to the library C:\emsdk\upstream\emscripten\system\lib. Should be able to Build and Run an executable as usual. – chickity china chinese chicken Commented Feb 2 at 6:19
Add a comment  | 

1 Answer 1

Reset to default -1

If you're willing to use Conan and CMake (I checked that VS has an extension for Conan, but I don't know if/how it works), then you can use the emsdk conan recipe; if not, at least go for CMake, it'll simplify things a lot for you. You need to make it a build dependency of your project's package, use virtualenv generator, and its conanfile will set up the whole toolchain. Ideally, this build dependency should be in a conan profile which you use to build your package and all its dependencies.

Doing the same thing manually, i.e. without a package manager (conan) and build system (cmake), is a good way to learn how things work under the hood, but very impractical for any kind of more complex codebase. The goal of these tools is to make it so you don't have to set up a toolchain manually with system-specific caveats everytime.

Nonetheless, if you really want to use emscripten toolchain directly with your minimal project, you can look into the conan recipe's emsdk/all/conanfile.py, method package_info, to see which environment variables it sets. Some of them like CC, CXX are specific to CMake/build system, others like EMSCRIPTEN are for emscripten's CMake toolchain and yet others like EM_CACHE for emcc itself. You'll also need to manually replicate relevant parts of that CMake toolchain file.

As to your command line, you should probably separate compilation to object files and linking. emcc/em++ follow clang flags to compile, with small additions like setting cache directory. However, there's a ton of important custom flags for linking into final output - you need to choose what sort of output that'll be.

Do you want a UMD export? Do you want a ES6 module? Do you want to load it asynchronously? Do you want a standalone .wasm file instead of .js loader and .wasm code? Or do you want to compile to JS only without WASM? All of that is described in documentation. How will you bind your C/C++ code to JS? If you go for embind, there's a link flag -bind. There's a ton of options. This is something you'll need to decide based on how you want to use your project's output, even if you use CMake.

发布评论

评论列表(0)

  1. 暂无评论