SHANGHAI—President Trump says he wants General Motors Co. GM -0.38% to stop building cars in China, its biggest market. That would make GM—already plagued by weak sales in the U.S.—vulnerable to setbacks in China too.

In today’s globalized car industry, auto makers need their factories to be close to their customers if they’re to turn a profit, a calculation that is being reinforced by the tit-for-tat tariffs China and the U.S. have imposed on each other’s exports, analysts say.

“Even if this tariff was zero, GM still wouldn’t build its China volumes in the U.S.,” said Robin Zhu, a senior analyst at Sanford C. Bernstein. “The supply chain would be too long, and logistics costs would make the cars structurally unprofitable.”

The only cars that make sense to export are high-end, low-volume vehicles, industry analysts say. But even Tesla Inc., which has specialized in that sort of vehicle until now, is planning to move away from exporting. It is building a factory in China as it aims to transition from niche startup to mainstream auto maker.

President Trump said he urged General Motors CEO Mary Barra to get back into Ohio, after the company announced Monday it plans to cut jobs and close several plants. Photo: AP

Currently, Tesla is doing what Mr. Trump wants GM to do—export American-made cars to China. But Tesla sales have been hammered by Chinese tariffs. Though China had reduced its tariff on autos to 15% from 25% in July, it then upped the tariff on U.S.-built vehicles to 40% in retaliation for the Trump administration’s new tariffs.

Tesla’s China sales fell 56% in the three months to October, compared with the same period last year. Until then, Tesla had been having a good year: it sold roughly 10,700 vehicles in China in the first half of 2018, up 90% year-over-year, according to research consulting firm LMC Automotive.

The third-quarter sales slump led Tesla to slash prices in China by up to 26% last week, as affluent Chinese consumers reached the limits of their tolerance for paying hefty markups on imports. With the price cut, a high-end Model X now costs roughly $173,000 in China, down from $226,000.

China’s tariffs on U.S.-built vehicles have hurt Tesla most of all, but they have also affected BMW AG , Daimler AG and Ford Motor Co.

Last month, Tesla said it was accelerating plans to build a plant in Shanghai, which will likely start producing vehicles around 2020-21, in response to the dispute between Beijing and Washington. It ultimately aims to make 500,000 cars a year in China, emulating GM and others in building vehicles locally to capitalize fully on Chinese demand.

“ The whimsical imposition of tariffs means no auto maker is likely to want to use the U.S. as an export base in the future ” — Janet Lewis, Macquarie Group

Higher tariffs are convincing auto makers to build more, not less, overseas, said Janet Lewis, head of industrials and transportation in Asia at Macquarie Group.

“Given GM’s customers are in China, it would make no economic sense to export from the U.S. to China,” Ms. Lewis said.

GM is heavily dependent on China: it sold 835,934 vehicles there in the third quarter, compared with 694,638 in the U.S. The two markets together contributed more than three quarters of GM’s global sales. GM banked roughly $2 billion in profits from its China operations last year.

GM said Monday it would cease production at several North American factories, citing weak sales for passenger sedans, as part of a restructuring that leaves GM’s China facilities untouched.

Mr. Trump voiced dismay at the decision.

“I think GM ought to stop making cars in China and make them here,” Mr. Trump said in an interview with The Wall Street Journal. “I think they forgot where they came from.”

GM’s spokeswoman in China didn’t respond to questions Tuesday about Mr. Trump’s remarks. Previously, GM Chief Executive Mary Barra said the restructuring was a necessary response to “changing market conditions and customer preferences.”

Newsletter Sign-up The 10-Point. A personal, guided tour to the best scoops and stories every day in The Wall Street Journal. PREVIEW

One big change in the auto industry is the shift from gasoline engines to battery-powered electric motors, a shift which China is leading. GM has pledged to invest heavily in electric vehicles and plans to launch 20 models in China by 2023.

The U.S. auto maker has a policy of “we build where we sell” to reduce supply-chain costs and avoid import tariffs, and it exports almost nothing from the U.S. to China, though it does export one China-built model, the Buick Envision, to the American market. GM has appealed for a tariff exemption for the Envision, which it says cannot be built economically in the U.S. since China accounts for 80% of the vehicle’s global sales.

The world’s major auto makers all have extensive manufacturing operations in China, reflecting the highly globalized nature of the industry. The U.S.-China trade dispute makes a shift in that globalized approach even less likely.

“The whimsical imposition of tariffs means no auto maker is likely to want to use the U.S. as an export base in the future,” said Macquarie Group’s Ms. Lewis.

Write to Trefor Moss at Trefor.Moss@wsj.com