Hello!

Saw, but did not find the necessary articles. Who has experience, how to make a site in C ++ and what are the pitfalls? Can I work with SQLITE?

Thank.

Closed due to the fact that it is necessary to reformulate the question so that it was possible to give an objectively correct answer by the participants of Kromster , lexxl , Streletz , user207618, Ivan Pshenitsyn 16 Aug '16 at 18:54 .

The question gives rise to endless debates and discussions based not on knowledge, but on opinions. To get an answer, rephrase your question so that it can be given an unambiguously correct answer, or delete the question altogether. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • 2
    site? can the server ?? - Gorets
  • 3
    possible, but why? - Zowie
  • eight
    Is it better for PHP? - VladislavMSK
  • 7
    @ VladislavMSK - java, c #, node.js, ruby, python ...... By the way, why C ++? ASM is better right away, machine code is even better, why is it that β€œtrifles”? By the way, from tz. just stupid site - yes, better in php - Zowie
  • four
    @ VladislavMSK, apparently you don’t own C ++ if you ask such questions. - Viacheslav

3 answers 3

You can but hemorrhoids. You can work with any DBMS. Though your own write.

I can tell only under windows. IIS web server.

Option 1:
easiest via CGI. The output of such std :: cout << "<br> <p> HTML </ p>";

Disadvantages: A separate process is launched for each request, and closes upon completion. This leads to a serious decrease in performance.

Option 2: Use the fastcgi protocol. Here the output will be through a function in ("<br> <p> HTML </ p>");


Testing.

hello world:

C++ (fastcgi) ~ 12000/сСк.(ΡΠ²Ρ‹ΡˆΠ΅ 12000 ΠΊΠΈΠ΄Π°ΡŽΡ‚ΡΡ ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΡ ΠΈΠ· w3wp.exe) php (fastcgi) - 6300/сСк. perl (isapi) - 2200/сСк. python (isapi) - 15280/сСк. (ΠΊΡΡˆΠΈΡ€ΠΎΠ²Π°Π½ΠΈΠ΅) python (cgi) - 74/сСк. 

150000 cycles with addition and multiplication operations:

 C++ - 5500/сСк.(long long) php - 220/сСк. Π² 25 Ρ€Π°Π· perl - 48/сСк. Π² 114 Ρ€Π°Π· python cgi - 34/сСк. Π² 161 Ρ€Π°Π· 

50,000 cycles . counter output i (network load)

 C++ - 950/сСк. php - 750/сСк. perl - 7/сСк. python cgi - 9/сСк. 

@nolka Probably 50,000 per second. I had load balancing for multiple servers. And php was launched not as fastcgi, but as cgi. But still the result is indicative.

Problems:

  1. no tools. You have to write yourself.

  2. not the fact that the finished site will work faster. If the level of the programmer is not very high, then most likely the performance will be much worse than the same site written in a specialized language ..

  3. hosting is not suitable. Need a virtual dedicated server.

  4. It is necessary to correctly implement separate processing of get, post requests.

  5. processing of file download (via post), image upload processing (for captcha for example).

  6. working with headers (sometimes you want to display a header depending on the results, at run time).

  7. handling ajax requests is also different. This should be taken into account both for the entire application (separate exe for ajax processing) and for post get processing via ajax. Again, all server variables come in different ways. And if you don’t know the http protocol well, then the code will have many patches.

  8. If the development environment does not support working in utf-8 encoding, then there will be many to_utf8 () to_ansi ().

  9. Encoding the urldecode () query string from different browsers is different. We'll have to use the curl + normal hex decoding bundle. When ajax request is better to wrap everything in urlencode from javascript. Naturally, if the site has a flash or another language, it will also encode everything in different ways. We'll have to get into the source of these flash and change everything there.

  10. There are no ready-made "scripts" for certain tasks for the server. Everything needs to be developed from scratch.

Basically, writing in C for the web is like writing gui in assembler.

Pros:

  1. With proper execution, the speed of the program will be very high. (for example, before the cycle of receiving requests, the process collects the required data from the disk, and puts them into memory, where it sorts and indexes). Connection with subd is established before the cycle of receiving requests.

  2. Get acquainted with the internal structure of the http protocol. Perhaps even with tcp / ip, dns, smtp, pop.

  3. With proper execution and configuration of the hosting, the site will not be able to hack. Hackers will have to study its structure. (And not like that: a vulnerability was found in joomla version 1.5.241, all users of this version should download the patch, otherwise hackers can ...)

  4. It all depends on the developer. No new version of the language should lead to problems. (A la in version 2.1507ab, outdated ones have been deleted ... or similar moments are impossible in this language.)

  5. There are no confidential information leaks. CMS (WordPress for example) may contain special instructions for transferring certain information to CIA data analysis centers or become inoperative at a certain moment.

Instructions: Windows. IIS web server. CGI protocol.

 1) БоздаСшь консольноС ΠΏΡ€ΠΈΠ»ΠΎΠΆΠ΅Π½ΠΈΠ΅ C++. 2) ΡƒΠ±ΠΈΡ€Π°Π΅ΠΌ ΠΏΡ€Π΅Π΄Π²Π°Ρ€ΠΈΡ‚Π΅Π»ΡŒΠ½ΠΎ скомпилированный Π·Π°Π³ΠΎΠ»ΠΎΠ²ΠΎΠΊ. 3) ЗапускаСм консоль IIS. сайты -> Π΄ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ Π²Π΅Π± сайт. имя: CGI ΠΏΡƒΡ‚ΡŒ: C:\inetpub\CGI ОК 

We go to this site in the console.

 4)Π”ΠΎΠΊΡƒΠΌΠ΅Π½Ρ‚ ΠΏΠΎ ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ: index.wpp 5)Π’ΠΈΠΏΡ‹ mine: .wpp text/html 6)Π”Π°Π»Π΅Π΅ Π² ΠΏΠ°ΠΏΠΊΠ΅ C:\inetpub\CGI создаСм пустой Ρ„Π°ΠΉΠ» index.wpp. 

7) In C ++ project we put the lines

 #include <iostream> int main() { std::cout << "\nΠŸΡ€ΠΈΠ²Π΅Ρ‚ ΠΌΠΈΡ€ Π‘++ Π² web!"; return 0; } 

8) Compile. Take the exe file. let's say this is CGI.exe

 ΠΊΠ»Π°Π΄Π΅ΠΌ Π² ΠΏΠ°ΠΏΠΊΡƒ C:\inetpub\CGI 

9) then go back to the IIS settings -> sites -> CGI -> handler mapping -> add module mapping.

 ΠΏΡƒΡ‚ΡŒ запроса: *.wpp ΠΌΠΎΠ΄ΡƒΠ»ΡŒ: cgimodule исполняСмый Ρ„Π°ΠΉΠ»: "C:\inetpub\CGI\CGI.exe" имя: Π³Π»Π°Π²Π½Ρ‹ΠΉ Π΄Π°. 

10) All now we go on ip which is specified in site bindings. The browser displays a welcome line.


Instructions: Windows. IIS web server. FASTCGI protocol.

 1) ΠšΠ°Ρ‡Π°Π΅ΠΌ libfcgi2 ΠΎΡ‚ΡΡŽΠ΄Π° http://www.coastrd.com/download Π²ΠΎΡ‚ это: DLL library + headers for C/C++, Delphi, VB & other BASICs 2) Из Π°Ρ€Ρ…ΠΈΠ²Π° ΠΏΠ°ΠΏΠΊΡƒ Π‘++ ΠΊΠ»Π°Π΄Π΅ΠΌ Π² ΠΏΠ°ΠΏΠΊΡƒ include срСды Ρ€Π°Π·Ρ€Π°Π±ΠΎΡ‚ΠΊΠΈ. Lib Π² ΠΏΠ°ΠΏΠΊΡƒ с lib Ρ„Π°ΠΉΠ»Π°ΠΌΠΈ, Π° dll Π² ΠΏΠ°ΠΏΠΊΡƒ Π‘:/include/fastcgi test 3) По этой ссылкС смотрим ΠΊΠ°ΠΊ ΡΠΊΠΎΠ½Ρ„ΠΈΠ³ΡƒΡ€ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ сам IIS http://www.coastrd.com/cgioniis7 Π² ΠΏΡ€ΠΈΠ½Ρ†ΠΈΠΏΠ΅ Π½Π΅ отличаСтся ΠΎΡ‚ CGI ΠΌΠ΅Ρ‚ΠΎΠ΄Π°, Π½Π°Π΄ΠΎ Ρ‚ΠΎΠ»ΡŒΠΊΠΎ Π²Ρ‹Π±Ρ€Π°Ρ‚ΡŒ ΠΌΠΎΠ΄ΡƒΠ»ΡŒ Π½Π΅ cgimodule, Π° fastcgimodule. 4) Π‘ΠΎΠ·Π΄Π°Π΅ΠΌ Π½ΠΎΠ²Ρ‹ΠΉ ΠΏΡ€ΠΎΠ΅ΠΊΡ‚ ΠΊΠΎΠ½ΡΠΎΠ»ΡŒΠ½Ρ‹ΠΉ.(Ρ‚ΠΎΠ»ΡŒΠΊΠΎ x32) Π’ Π½Π΅Π³ΠΎ добавляСм Ρ‚Π°ΠΊΠΈΠ΅ строчки #ifndef _FCGXAPP_H #include <libfcgi2.h> #endif #pragma comment(lib, "libfcgi2.lib") #include <iostream> // функция Π²Ρ‹Π²ΠΎΠ΄Π° static inline void v(const std::string & text , FCGX_REQUEST & FCGI) { FCGX_PutStr( text.data(), text.size(), FCGI.pOut); } static inline std::string int_to_string(int integ) { if(integ == 0){ return "0"; } char * buffer = new char [32]; _itoa_s(integ, buffer,32, 10 ); return buffer; } static inline void v(const int & integ , FCGX_REQUEST & FCGI) { std::string global_string = int_to_string(integ); FCGX_PutStr( global_string.data(), global_string.size(), FCGI.pOut); } int main() { // счётчик запросов long long count_fastcgi = 0; FCGX_REQUEST FCGI = { 0 }; FCGX_InitRequest( &FCGI, 0, 0 ); std::string first = "Content-Type: text/html;charset=utf-8\r\nX-Powered-By: C++\r\nAccess-Control-Allow-Origin: *\r\n\r\n"; const char * first_string = first.data(); int size_ = first.size(); //first+="Access-Control-Allow-Origin: *\r\n\r\n"; while( FCGX_Accept_r(&FCGI) == 0 ) { // Π’Ρ‹Π²ΠΎΠ΄ Π·Π°Π³ΠΎΠ»ΠΎΠ²ΠΊΠΎΠ². FCGX_PutStr( first.data(), size_, FCGI.pOut ); // ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Ρ‹Π΅ сСрвСра for(int i = 0; i < FCGI.nParam ; i++) { v("<br>" , FCGI); v(FCGI.envp[i] , FCGI); } } // Ρ†ΠΈΠΊΠ» return 0; } 5) ПослС компиляции ΠΊΠ»Π°Π΄Π΅ΠΌ fastcgi.exe Π² ΠΏΠ°ΠΏΠΊΡƒ Π‘:/include/fastcgi test ДобавляСм сопоставлСниС модуля Π² IIS. Π’ΠΎΠΆΠ΅ самоС ΠΊΠ°ΠΊ Π² cgi, Ρ‚ΠΎΠ»ΡŒΠΊΠΎ ΡƒΠΊΠ°Π·Π°Ρ‚ΡŒ Π½Π΅ cgimodule, Π° fastcgimodule. 

If everything worked, we take apache jmeter (which by the way is written on java -> terribly buggy), set the full optimization in the compiler and see how many pages it can process. The main thing is that this jmeter itself does not hang.

  • And what is Yandex and Google written on? It is clear that not on PHP, on Java or what? This is unrealistic, it’s very sluggish - VladislavMSK
  • facepalm .... - KoCaTKo
  • Oh, how much java is in Google, it’s time to know that C ++ is on their back end - vanekk1
  • If you look at Yandex vacancies, you can see that C ++ is also there. - Lucky
  • 3
    Mnogabukoff. Neither asilil, but cool! Plusadynadyn. - gecube

You can use a C ++ framework such as CppCMS

  • Is this something completely new? @pustreg, I read about 5 minutes on your link, and did not understand, CppCMS itself is an http server or what? - avp
  • You know, if you write a site in C ++, then it should have its own engine. Otherwise, what's the point? - manking September
  • 2
    @manking, and all the basic functions of PHP (which means joomla), aren't they written in C? Is it really necessary to refer to them a layer of interpretation? - avp
  • 2
    "You know, if you write a site in C ++, then it should have its own engine." And on what fuel will this engine work? Or is it electric? - alexlz
  • 3
    The simple output of hello world in PHP is 500 pps. on fastcgi C ++ 50000 p / sec. All kernels are 100% clogged. tested 2 years ago on apache jmeter. It is clear that php already parses the incoming request and creates post get arrays, and other moments. But if you competently create only those components that are needed by the site, then for a large company you will get a huge savings on the servers. I think most serious projects, and not only on the web, are written in C / C ++, plus their own engine. - manking

I recommend the library Wt . Templates, sessions, translations into the client's language. Work with DBMS, JSON, AJAX and COMET. Russian documentation is really not enough.

Also recommend crow .

Recently, I developed a javascript template engine and a compiler for json generation modules for lighttpd on pure C: mod_json-dev .

  • Full site will be possible to blind? Interesting, I'll try. - VladislavMSK
  • I myself recently work with it, I write browser games, I am surprised at the breadth of possibilities. Passed several pitfalls, there will be problems - ask. I recommend to immediately install the latest version, which is obsolete in the Debian. - sercxjo
  • one
    in addition, there are crow / http2 frameworks that support routing and middlevary interfaces. There are lots of libraries to work with sql (I prefer sqlp11) - strangeqargo
  • 2
    @strangeqargo where were you a couple of months ago :) when I decided to make my template engine + json generator on pure C. - sercxjo
  • one
    @ VladislavMSK on wt, sure you can. on crow / cpphttp2 + rapidJSON (the fastest now, it seems) can at least be done, for example, api for the frontend (I am having fun with it). Crow cannot by default distribute static, but middleware is written very quickly for this (did, then I’ll post it in githabiki if need be) - strangeqargo